Excel vba 从另外一个工作簿提取数据 最快的方法

2024-12-15 22:44:19
推荐回答(1个)
回答1:

试试这段代码,对应修改:
Sub Macro1()
Dim cnn As Object, SQL$, sh As Worksheet
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider = Microsoft.Jet.Oledb.4.0;Extended Properties ='Excel 8.0;hdr=no';Data Source =" & ThisWorkbook.Path & "\文档2.xls"
For Each sh In Sheets
SQL = "Select f2,f5,f6 from [" & sh.Name & "$a2:f]"
sh.Range("A2:C65536").ClearContents
sh.[a2].CopyFromRecordset cnn.Execute(SQL)
Next
cnn.Close
Set cnn = Nothing
End Sub