2017-08-09 200 views
2

以下是我发生的问题。我使用MS Excel 2013.Excel 2013无法在ThisWorkbook目录中找到并打开文件

使用下面的宏,我试图找到那些帐户(符合条件“范围内”,例如帐户12345678),复制它们以搜索相同的文件夹(其中ThisWorkbook是) ,找到另一个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 
+2

最有可能你缺少ThisWorkbook.Path'之间'的“\”分隔符和文件名 –

+1

什么@ShaiRado说。解决该问题后:'Sheets()'隐式引用'ActiveWorkbook'。执行Workbooks.Open时,新打开的工作簿将成为“ActiveWorkbook”。阅读[这里](https://stackoverflow.com/documentation/excel-vba/1576/common-mistakes/5110/qualifying-references)了解更多信息。您也可以通过阅读[this](https://stackoverflow.com/documentation/excel-vba/1107/vba-best-practices/9292/avoid-using-select-or-activate)来避免很多问题。快速阅读,不幸的是文档正在消失。 – FreeMan

+0

@FreeMan - 我知道我的代码并不像应该那么优雅,但我是VBA的初学者。下一步的改进将会出现!谢谢! ;) – VSE

回答

0

尝试下面的代码,我有我的解释,并在代码中你的问题(如commnets):

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 
+0

关于Sheet2 - 根据VBA项目对象,它是Sheet2(报告),在新打开的文件中,编码试图在相同的目录中找到。 关于粘贴 - 一旦我从新的opend文件中复制单元格,我想返回到ThisWorkbook,该宏所在的位置,并将该选择粘贴到初始excel文件(ThisWorkbook)内的另一个表单中。 关于“Loop”&“sDir = Dir $” - 抱歉,我没有删除它。它留在我以前尝试的代码中。请忽略它! – VSE

+0

@VSE所以当你尝试编码代码时会发生什么? (刚刚编辑它) –

+0

@ Shai Rado - 当我运行代码时,您提出了以下消息:Object不支持此属性或方法(错误438)。 – VSE

相关问题