|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
zhaogang1960 发表于 2012-5-18 17:51
SQL = "select * from [Sheet2$B4:M]" & t & " union all select * from [Sheet3$B4:M]" & t & " union a ...
老师,我依葫芦画瓢,套用了,但有一个问题,单号不能查询,请帮我看看
- Sub 明细()
- Dim cnn As Object, rs As Object, SQL$
-
-
- If [a2] <> "" Then s = s & " and 单号 ='" & [a2] & "'"
- If [b2] <> "" Then s = s & " and 辅助列 ='" & [b2] & "'"
- If [c2] <> "" Then s = s & " and 仓库 ='" & [c2] & "'"
-
- If s <> "" Then t = " where " & Mid(s, 5)
- Set cnn = CreateObject("Adodb.Connection")
- Set rs = CreateObject("Adodb.Recordset")
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ActiveWorkbook.FullName
-
-
- SQL1 = "select '入库' as 收支,日期,单号,单据类型,辅助列,物料名称,规格型号,单位,数量,仓库,备注 from [入帐$a:k] where 品号 is not null union all select '出库' as 收支,日期,单号,单据类型,辅助列,物料名称,规格型号,单位,数量,仓库,备注 from [出帐$a:k] where 品号 is not null"
- SQL2 = "select * from (" & SQL1 & ")" & t & " union all select * from (" & SQL1 & ")" & t
- SQL = "select 收支,日期,单号,单据类型,辅助列,物料名称,规格型号,单位,数量,仓库,备注 from (" & SQL2 & ") order by 日期"
-
- rs.Open SQL, cnn, 1, 3
- Range("a5:l10000").ClearContents
- Range("a5").CopyFromRecordset rs
-
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
-
- End Sub
复制代码 |
|