|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test1()
- Dim f As String
- With Application.FileDialog(msoFileDialogOpen) '1
- .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 Conn As Object, SQL As String
- Dim ar, br(1 To 2) As String, i As Integer
-
- Worksheets(1).Activate
- ar = Range("A4", Range("A4").End(xlToRight))
- ar = WorksheetFunction.Transpose(WorksheetFunction.Transpose(ar))
-
- For i = 1 To UBound(ar)
- ar(i) = Replace(Replace(Replace(ar(i), "[", "("), "]", ")"), ".", "#")
- If i > 2 Then
- ar(i) = "b.[" & ar(i) & "]"
- Else
- br(i) = "[" & ar(i) & "]"
- ar(i) = "a.[" & ar(i) & "]"
- End If
- Next
- Set Conn = CreateObject("ADODB.Connection")
- Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
-
- SQL = "SELECT " & Join(ar, ",") & " FROM " & _
- "[" & ActiveSheet.Name & "$A4:B" & Cells(Rows.Count, 2).End(xlUp).Row & "] a " & _
- "LEFT JOIN " & _
- "[Excel 12.0;Database=" & f & "].[$A1:IV] b " & _
- "ON a." & br(1) & "=b." & br(1) & " AND a." & br(2) & "=b." & br(2)
-
- Range("A5").CopyFromRecordset Conn.Execute(SQL)
-
- Conn.Close
- Set Conn = Nothing
- Beep
- End Sub
复制代码 |
|