2016-07-14 56 views
0

探數據我有兩個工作簿,打電話給他們第一冊和第二冊。 Book1已打開,我正試圖從已關閉的Book2中將數據存入Book1。 Book1包含列出Book2中相應元素的列。例如,Book1有一列數字對應於Book2中的另一個數字列表。我正在嘗試使用VBA從Book2中的行中獲取數據,並將匹配的數字與Book1中的數據進行匹配。錯誤91(對象未設置)當關閉的工作簿

現在,我有以下MWE產生錯誤91(對象未設置),當我嘗試執行代碼:因爲它是

Dim path As String, book As String, sheet As String 
Dim targetRng As Range, sourceRng As Range 

path = Left(Cells(2, 1).Value, InStrRev(Cells(2, 1).Value, "\")) 
book = Dir(Cells(2, 1).Value) 
sheet = Cells(2, 2).Value 
Set targetRng = Range("A1").CurrentRegion 
Set targetRng = targetRng.Offset(1, 0).Resize(targetRng.Rows.Count - 1) 'Ignore header row 
For i = 1 To targetRng.Rows.Count 
    Set sourceRng = "'" & path & "[" & book & "]" & sheet & "'!" & Range("A:A").Find(targetRng.Cells(i, 1).Value) 'Error is here 
    targetRng.Cells(i, 2).Value = ExecuteExcel4Macro("'" & path & "[" & book & "]" & sheet & "'!" & Cells(sourceRng.Row, 2).Address) 
    ' 
    'Do some other stuff 
    ' 
Next i 
+2

我不能告訴你想要做什麼,但ExecuteExcel4Macro功能是不會返回一個範圍,所以你不能設置'sourceRng'等於宏的結果。 – OpiesDad

+0

啊,這只是複製和粘貼時的一個錯誤。我是在此基礎上回答我的代碼:http://stackoverflow.com/questions/18069445/vba-excel-query-a-closed-workbook-without-opening-it –

+0

那是什麼原因造成的錯誤。你糾正它,並得到一個新的錯誤? – OpiesDad

回答

0

我不願意提供使用ADO答案一個廣泛的話題。但我會爲您提供兩個如何使用ADO查詢Excel文件的示例。

在左邊,我們有,我們將查詢[Employees.xlsx]源文件。在我們正在更新的工作簿的右側[Sample.xlsm]。在我的例子中,我們使用了展位工作簿的[Sheet1]。請注意,ADO將使用列標題作爲字段名稱(id,first_name,last_name ...等)。

enter image description here

在例1中我們查詢[Employees.xlsx]加載從[Employees.xlsx]![工作表Sheet1]所有記錄到記錄EmployeeData。接下來,我們遍歷[Sample.xlsm]![Sheet1]的id列並設置記錄集EmployeeData.Filter = "id=" & Cells(x, 1)的過濾器。然後記錄字段的值複製到相應的單元格.Cells(x, 2) = EmployeeData.Fields("first_name")

Sub Example1() 

    Dim lastRow As Long, x As Long 

    Const adOpenKeyset = 1 
    Const adLockOptimistic = 3 
    Dim conn 
    Dim EmployeeData 

    Set conn = CreateObject("ADODB.Connection") 
    conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=I:\stackoverflow\Employees.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";" 
    conn.Open 

    On Error GoTo CloseConnection 

    Set EmployeeData = CreateObject("ADODB.Recordset") 

    With EmployeeData 
     .ActiveConnection = conn 
     .CursorType = adOpenKeyset 
     .LockType = adLockOptimistic 
     .Source = "Select * FROM [Sheet1$]" 
     .Open 
     On Error GoTo CloseRecordset 

    End With 

    With Worksheets("Sheet1") 
     lastRow = .Range("A" & Rows.Count).End(xlUp).Row 
     For x = 2 To lastRow 

      EmployeeData.Filter = "id=" & Cells(x, 1) 
      If Not (EmployeeData.BOF And EmployeeData.EOF) Then 
       .Cells(x, 2) = EmployeeData.Fields("first_name") 
       .Cells(x, 3) = EmployeeData.Fields("last_name") 
       .Cells(x, 4) = EmployeeData.Fields("email") 
       .Cells(x, 5) = EmployeeData.Fields("gender") 
       .Cells(x, 6) = EmployeeData.Fields("ip_address") 
      End If 

     Next 
    End With 
CloseRecordset: 

    EmployeeData.Close 
    Set EmployeeData = Nothing 

CloseConnection: 
    conn.Close 
    Set conn = Nothing 
End Sub 

enter image description here

在例題我們LEFT JOIN [Sample.xlsm]![工作表Sheet1]與[Employees.xlsx]![Sheet1中。通過這種方式,我們使用CopyFromRecordset爲我們更新數據。
Sheet1.Range("A2").CopyFromRecordset EmployeeData

Sub Example2() 

    Dim lastRow As Long, x As Long 

    Const adOpenKeyset = 1 
    Const adLockOptimistic = 3 
    Dim conn 
    Dim EmployeeData 

    Set conn = CreateObject("ADODB.Connection") 
    conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";" 
    conn.Open 

    On Error GoTo CloseConnection 

    Set EmployeeData = CreateObject("ADODB.Recordset") 

    With EmployeeData 
     .ActiveConnection = conn 
     .CursorType = adOpenKeyset 
     .LockType = adLockOptimistic 

     .Source = "SELECT table1.id, table2.[first_name], table2.[last_name], table2.[email], table2.[gender], table2.[ip_address] FROM ([Sheet1$] table1 LEFT JOIN `I:\stackoverflow\Employees.xlsx`.`Sheet1$` table2 ON table2.[id]=table1.[id])" 
     .Open 
     On Error GoTo CloseRecordset 


    End With 

    Sheet1.Range("A2").CopyFromRecordset EmployeeData 

CloseRecordset: 

    EmployeeData.Close 
    Set EmployeeData = Nothing 

CloseConnection: 
    conn.Close 
    Set conn = Nothing 
End Sub 

注: CopyFromRecordset將替換受影響的列中的所有數據。出於這個原因,建議使用示例1,直到您開始編寫查詢爲止。

相關問題