2017-08-09 139 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

相關問題