|
楼主 |
发表于 2019-1-26 13:23
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 opiona 于 2019-1-26 13:25 编辑
工参考:
- Sub TEST()
-
- path1 = ThisWorkbook.Path & "\工作簿1.xlsx"
- path2 = ThisWorkbook.Path & "\工作簿2.xlsx"
-
- StrSQL = StrSQL & "SELECT [客户编码],[客户名称],[收入版块]"
- StrSQL = StrSQL & ",SUM([前月毛利率]) AS [前月毛利率]"
- StrSQL = StrSQL & ",SUM([上月毛利率]) AS [上月毛利率]"
- StrSQL = StrSQL & " FROM ("
-
- StrSQL = StrSQL & "SELECT [客户编码],[客户名称],[收入版块]"
- StrSQL = StrSQL & ",[毛利率C] AS [前月毛利率]"
- StrSQL = StrSQL & ",[毛利率D] AS [上月毛利率]"
- StrSQL = StrSQL & " FROM [Excel 12.0;HDR=YES;Database=" & path1 & "].[工作表名1$]"
-
- StrSQL = StrSQL & " UNION ALL "
- StrSQL = StrSQL & " SELECT [客户编码],[客户名称],[收入版块]"
- StrSQL = StrSQL & ",[毛利率A] AS [前月毛利率]"
- StrSQL = StrSQL & ",[毛利率B] AS [上月毛利率]"
-
- StrSQL = StrSQL & " FROM [Excel 12.0;HDR=YES;Database=" & path2 & "].[工作表名2$]"
-
- StrSQL = StrSQL & ") GROUP BY [客户编码],[客户名称],[收入版块]"
-
- Str_coon = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';Data Source =" & ThisWorkbook.FullName
-
- End Sub
复制代码 |
|