簡體   English   中英

如何在 VBA 中使用 SQL 將兩個工作簿與一個公共列合並

[英]How to merge two workbooks with one common column using SQL in VBA

我相信我也可以通過宏來做到這一點,但我想在 VBA 中介紹 ADODB 和 SQL 查詢。 數據集不在 Access 中,而是在單獨的工作簿中,兩個工作簿的 SQL 查詢如下:

SELECT getRollpHierRpt.Field1, ['App].[Master Book Name], ['App].[App Book Name], ['App].[Secondary App Book Name], ['App].[App Code], ['App].[App Book Status], ['App].[Book Transit], ['App].[Transit Desc], ['App].[Legal Entity Id], ['App].[Legal Entity Desc]
FROM ['App] INNER JOIN getRollpHierRpt ON ['App].[Book Transit] = getRollpHierRpt.Field1;

我在互聯網上看到的例子包括連接到 Access/SQL 服務器,但如果數據集是兩個 excel 工作簿?

Sub MergeIt()
Dim conn1 As New ADODB.Connection
Dim conn2 As New ADODB.Connection
 With conn1
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source=C:\Users\amzubaid\Desktop\Practice Merging\13-10-2017.xlsm;" & 
"Extended Properties=""Excel 12.0 Macro;HDR=YES';IMEX=1"""
.Open
  End With
With conn2
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source=C:\Users\amzubaid\Desktop\Practice Merging\20181015-Practice.xlsm;" & "Extended Properties=""Excel 12.0 Macro;HDR=YES';IMEX=1"""
  .Open
 End With
Dim rsSmall As New Recordset
rsSmall.Open "select [Field1] from [getRollpHierRpt$]", conn1
 Dim r As Range
Dim rsBig As New Recordset
Do
Set r = Range("a" & Rows.Count).End(xlUp).Offset(1, 0) 'first unoccupied cell

rsBig.Open "select [App Code],[Legal Entity Desc]," & rsSmall("Field1") & " 
 As GetRollPH from [getRollpHierRpt$] where [BookTransit] = " & 
 rsSmall("Field1"), conn2
 r.CopyFromRecordset rsBig
rsBig.Close
rsSmall.MoveNext
Loop Until rsSmall.EOF  'end if file
rsSmall.Close
conn1.Close
conn2.Close

End Sub

您需要設置與每個工作簿的連接。 實際上,這依賴於將每個工作簿視為數據庫中的一個表。 每個工作簿中都必須有列標題,它們被視為字段名稱。 試試這個了解詳情

好的 所以這似乎不可能 - 所以一種可能的解決方法是打開較小的表,然后單步執行它並使用各個值從較大的表中檢索所需的記錄:這是一個使用名為 Testfile1 和 testfile2 的工作簿的示例。 每個都包含一個名為“test”的工作表和一些在名為“TestC”和“TestD”的 testfile2 中帶有列標題的數據,以及名為“TestA”的 testfile1 中的第一個(鏈接)文件。 (這需要引用 VBE、工具、參考中的 Microsoft Active X 數據對象”庫)

Sub TwoFiles()
Dim conn1 As New ADODB.Connection
Dim conn2 As New ADODB.Connection
With conn1
    .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\testfile1.xlsm;" & "Extended Properties='Excel 12.0 Macro;HDR=YES';"
    .Open
End With
With conn2
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\testfile2.xlsm;" & "Extended Properties='Excel 12.0 Macro;HDR=YES';"
.Open
End With
Dim rsSmall As New Recordset
rsSmall.Open "select [testA] from [test$]", conn1
Dim r As Range
Dim rsBig As New Recordset
Do
Set r = Range("a" & Rows.Count).End(xlUp).Offset(1, 0) 'first unoccupied cell

rsBig.Open "select [TestC],[TestD]," & rsSmall("testA") & " As GetRollPH from [test$] where [testC] = " & rsSmall("testA"), conn2
r.CopyFromRecordset rsBig
rsBig.Close
rsSmall.MoveNext
Loop Until rsSmall.EOF  'end if file
rsSmall.Close
conn1.Close
conn2.Close


End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM