|
请参考:
- Sub ADO联合查询()
- Dim cnn As Object, rs As Object, SQL$, i&, myPath$, MyFile$
- myPath = ThisWorkbook.Path & ""
- Set cnn = CreateObject("adodb.connection")
- cnn.Open ConnectionString:="Provider=MSDASQL;Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" & ThisWorkbook.Path
- ' cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='text;FMT=DELIMITED';Data Source=" & ThisWorkbook.Path'或
- MyFile = Dir(myPath & "*.csv")
- Do While MyFile <> ""
- If SQL = "" Then SQL = "select * from " & MyFile Else SQL = SQL & " union all select * from " & MyFile
- MyFile = Dir()
- Loop
- ' SQL = "select p_Code,NameID,AddessID,sum(金额) as 金额合计 from (" & SQL & ") group by p_Code,NameID,AddessID"'汇总金额
- Set rs = cnn.Execute(SQL)
- Cells.ClearContents
- For i = 1 To rs.Fields.Count
- Cells(1, i) = rs.Fields(i - 1).Name
- Next
- [a2].CopyFromRecordset rs
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|