Vba Excel 2013无法在此工作簿目录中找到并打开该文件

Vba Excel 2013无法在此工作簿目录中找到并打开该文件,vba,excel,Vba,Excel,我想到了以下问题。我使用MS Excel 2013 使用下面的宏,我试图找到这些帐户(符合“范围内”的条件,例如帐户12345678),复制它们,在同一文件夹(此工作簿所在的位置)中搜索,找到另一个以帐户编号命名的excel文件(例如“12345678.xlsx”)并打开它 在下面建议的更正之后,“我的宏”将查找并打开所需的文件。但现在的问题是,无法对其执行任何操作:复制、粘贴等 你能帮个忙吗 Sub FileFinder() 'Excel variables: Dim RngS As Exc

我想到了以下问题。我使用MS Excel 2013

使用下面的宏,我试图找到这些帐户(符合“范围内”的条件,例如帐户12345678),复制它们,在同一文件夹(此工作簿所在的位置)中搜索,找到另一个以帐户编号命名的excel文件(例如“12345678.xlsx”)并打开它

在下面建议的更正之后,“我的宏”将查找并打开所需的文件。但现在的问题是,无法对其执行任何操作:复制、粘贴等

你能帮个忙吗

Sub FileFinder()

'Excel variables:
Dim RngS As Excel.Range
Dim wbResults As Workbook

'Go to the column with specific text
Worksheets("Accounts source data").Activate
X = 3
Y = 25
While Not IsEmpty(Sheets("Accounts source data").Cells(X, Y))
    Sheets("Accounts source data").Cells(X, Y).Select
    If ActiveCell = "In scope" Then
        Sheets("Accounts source data").Cells(X, Y - 22).Select
        'Copy the account in scope
        Set RngS = Selection
        Selection.Copy
        'Search, in same directory where the file is located, the file with that account (file comes with account number as name)

        sDir = Dir$(ThisWorkbook.Path & "\" & RngS & ".xlsx", vbNormal)
        Set oWB = Workbooks.Open(ThisWorkbook.Path & "\" & sDir)
        'Here is where my error occurs 
        '[Run-time error 5: Invalid procedure call or argument]
        Sheet2.Cells("B27:B30").Copy
        oWB.Close
        End If
    X = X + 1
Wend

End Sub   

请尝试下面的代码,代码中有我的解释和问题(如Commnet):

选项显式
子文件查找器()
'Excel变量:
将结果设置为工作簿
将oWB设置为工作簿
将Sht变暗为工作表
变暗RNG As范围
将sDir设置为字符串
最后一排一样长
暗我一样长,深谷一样长
Col=25
'设置此工作簿对象
设置wbResults=ThisWorkbook
'设置工作表对象
Set Sht=工作表(“账户源数据”)
用短发
'查找列“Y”中包含数据的最后一行(列=25)
LastRow=.Cells(.Rows.Count,25).End(xlUp).Row
对于i=3到最后一行
如果.Cells(i,Col)=“范围内”,则
'直接设置范围,无需使用'Select'和'Selection'`
设置RngS=.Cells(i,Col).Offset(,-22)
'在文件所在的同一目录中搜索具有该帐户的文件(该文件以帐户号作为名称)
sDir=Dir$(ThisWorkbook.Path&“\”&RngS.Value&“.xlsx”,vbNormal)
设置oWB=Workbooks.Open(ThisWorkbook.Path&“\”&sDir)
oWB.工作表(“报告”).范围(“B27:B30”).副本

“***粘贴到此工作簿,在我的exmaple“Sheet2”中,您很可能缺少
此工作簿.Path
和@ShaiRado所说的文件名之间的“\”分隔符。解决该问题后:
Sheets()
隐式引用
ActiveWorkbook
。执行
工作簿的那一刻。打开
新打开的工作簿将成为
活动工作簿
。阅读更多信息。你们也可以通过阅读来避免很多问题。快速阅读,遗憾的是文档正在消失。@FreeMan-我知道我的编码并没有它应有的优雅,但我是VBA的初学者。接下来肯定会有改进!谢谢!;)@Mat'sMug-因此,请快速阅读,遗憾的是文档正在消失。:)@FreeMan关于Sheet2-根据VBA项目对象,它是Sheet2(报告),位于新打开的文件中,编码试图在同一目录中找到该文件。关于粘贴-复制新opend文件中的单元格后,我想返回宏所在的此工作簿,并将所选内容粘贴到初始excel文件(此工作簿)中的另一个工作表中。关于“Loop”和“sDir=Dir$”-对不起,我没有删除它。它留在了我以前尝试的代码中。请不要理它@VSE那么当您尝试edoted代码时会发生什么?(刚刚编辑过)@Shai Rado-当我运行代码时,您建议,出现以下消息:对象不支持此属性或方法(错误438)。@VSE如果不告诉我您在哪一行收到此错误,我几乎无能为力@Shai Rado“oWB.Sheet2.Range(“B27:B30”).Copy”-在读取后
Option Explicit

Sub FileFinder()

' Excel variables:
Dim wbResults As Workbook
Dim oWB As Workbook
Dim Sht As Worksheet
Dim RngS As Range
Dim sDir As String

Dim LastRow As Long
Dim i As Long, Col As Long

Col = 25

' set ThisWorkbook object
Set wbResults = ThisWorkbook

' set the worksheet object
Set Sht = Worksheets("Accounts source data")
With Sht
    ' find last row with data in Column "Y" (Col = 25)
    LastRow = .Cells(.Rows.Count, 25).End(xlUp).Row

    For i = 3 To LastRow
        If .Cells(i, Col) = "In scope" Then
            ' Set the range directly, no need to use `Select` and `Selection`
            Set RngS = .Cells(i, Col).Offset(, -22)

            ' Search, in same directory where the file is located, the file with that account (file comes with account number as name)
            sDir = Dir$(ThisWorkbook.Path & "\" & RngS.Value & ".xlsx", vbNormal)
            Set oWB = Workbooks.Open(ThisWorkbook.Path & "\" & sDir)

            oWB.Worksheets("Report").Range("B27:B30").Copy

            ' *** Paste in ThisWorkbook, in my exmaple "Sheet2" <-- modify to your needs
            wbResults.Worksheets("Sheet2").Range("C1").PasteSpecial Paste:=xlPasteAll, Transpose:=True

            oWB.Close SaveChanges:=False
'            sDir = Dir$

            ' clear objects
            Set RngS = Nothing
            Set oWB = Nothing
        End If
    Next i
End With

End Sub