|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub VBA_SQL()
- Dim AdoConn As New ADODB.Connection
- Dim strConn As String
- Dim strSQL As String
- strConn = " Provider=Microsoft.ACE.OLEDB.12.0;" & _
- "Data Source=" & ThisWorkbook.FullName & _
- ";Extended Properties=""Excel 12.0;HDR=YES"";"
- strSQL = "Select * FROM [EXCEL 12.0;HDR=YES;DATABASE=" & ThisWorkbook.Path & Application.PathSeparator & _
- "USER 1.xlsx;" & "].[USER$]" & vbCrLf & "UNION ALL" & vbCrLf & _
- "Select * FROM [EXCEL 12.0;HDR=YES;DATABASE=" & ThisWorkbook.Path & Application.PathSeparator & _
- "USER 2.xlsx;" & "].[USER$]" & vbCrLf & "UNION ALL" & vbCrLf & _
- "Select * FROM [EXCEL 12.0;HDR=YES;DATABASE=" & ThisWorkbook.Path & Application.PathSeparator & _
- "USER 3.xlsx;" & "].[USER$]" & vbCrLf & "UNION ALL" & vbCrLf & _
- "Select * FROM [EXCEL 12.0;HDR=YES;DATABASE=" & ThisWorkbook.Path & Application.PathSeparator & _
- "USER 4.xlsx;" & "].[USER$]"
- AdoConn.Open strConn
- Sheets(1).Range("A2").CopyFromRecordset AdoConn.Execute(strSQL)
- AdoConn.Close
- End Sub
- Sub VBA_SQL_1()
- Dim AdoConn As New ADODB.Connection
- Dim strConn As String
- Dim strSQL As String
- strConn = " Provider=Microsoft.ACE.OLEDB.12.0;" & _
- "Data Source=" & ThisWorkbook.FullName & _
- ";Extended Properties=""Excel 12.0;HDR=YES"";"
- strSQL = "Select * FROM [" & ThisWorkbook.Path & Application.PathSeparator & _
- "USER 1.xlsx;" & "].[USER$]" & vbCrLf & "UNION ALL" & vbCrLf & _
- "Select * FROM [" & ThisWorkbook.Path & Application.PathSeparator & _
- "USER 2.xlsx;" & "].[USER$]" & vbCrLf & "UNION ALL" & vbCrLf & _
- "Select * FROM [" & ThisWorkbook.Path & Application.PathSeparator & _
- "USER 3.xlsx;" & "].[USER$]" & vbCrLf & "UNION ALL" & vbCrLf & _
- "Select * FROM [" & ThisWorkbook.Path & Application.PathSeparator & _
- "USER 4.xlsx;" & "].[USER$]"
- AdoConn.Open strConn
- Sheets(1).Range("A2").CopyFromRecordset AdoConn.Execute(strSQL)
- AdoConn.Close
- End Sub
- Sub VBA_SQL_2()
- Dim AdoConn As New ADODB.Connection
- Dim strConn As String
- Dim strSQL As String
- strConn = " Provider=Microsoft.ACE.OLEDB.12.0;" & _
- "Data Source=" & ThisWorkbook.FullName & _
- ";Extended Properties=""Excel 12.0;HDR=YES"";"
- strSQL = "Select * FROM [D:\USER 1.xlsx;" & "].[USER$]" & vbCrLf & "UNION ALL" & vbCrLf & _
- "Select * FROM [D:\USER 2.xlsx;" & "].[USER$]" & vbCrLf & "UNION ALL" & vbCrLf & _
- "Select * FROM [D:\USER 3.xlsx;" & "].[USER$]" & vbCrLf & "UNION ALL" & vbCrLf & _
- "Select * FROM [D:\USER 4.xlsx;" & "].[USER$]"
- AdoConn.Open strConn
- Sheets(1).Range("A2").CopyFromRecordset AdoConn.Execute(strSQL)
- AdoConn.Close
- End Sub
复制代码 VBA链接多个工作簿汇总的三种写法,留着总有用上的时候。
|
|