|
各位大哥大姐我刚开始接触VBA我想知道4楼师傅的代码里修改哪里可以变化另一个工作簿里匹配列位置,修改哪里可以改变本工作簿咧列位置,原来使用公式看的懂,这个我真的看不懂
Option Explicit
Sub test1()
Dim f$
'f = ThisWorkbook.Path & Application.PathSeparator & "1.xlsx" '指定 工作簿
With Application.FileDialog(msoFileDialogOpen) '选择 工作簿
.InitialFileName = ThisWorkbook.Path
With .Filters
.Clear
.Add "Excel Files", "*.xls?"
End With
.AllowMultiSelect = False
If .Show Then f = .SelectedItems(1) Else Exit Sub
End With
Dim Cn As Object, Sq$(2)
Set Cn = CreateObject("ADODB.Connection")
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=no';Data Source=" & ThisWorkbook.FullName
Sq(0) = "[" & ActiveSheet.Name & "$E2:E" & Cells(Rows.Count, 5).End(xlUp).Row & "]a"
Sq(1) = "[Excel 12.0;HDR=no;Database=" & f & "].[$A2:Y]b"
Sq(2) = "SELECT b.f1,b.f2,b.f3,b.f4 FROM " & Sq(0) & " LEFT JOIN " & Sq(1) & " ON a.f1=b.f19"
Range("F2").CopyFromRecordset Cn.Execute(Sq(2))
Cn.Close: Set Cn = Nothing
MsgBox " ok!", 64
End Sub |
|