|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 小锋子 于 2019-7-12 12:50 编辑
老师们,请教一个问题:关于多条件查找,如果其中为空则不进入查找的条件内;
代码1 这个只能实现时间段内查找
- Sub TJ()
- On Error Resume Next '忽略错误值
-
- Dim cnn As Object, Sql$, shnm$, arr, i&, j&
- Set cnn = CreateObject("Adodb.Connection")
- shnm = Sheet7.Name
- ks = CDate([L5].Value)
- js = CDate([N5].Value)
- kh = [L2]
- ck = [N2]
- cnn.Open "provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;';data source=" & ThisWorkbook.FullName
- Sql = ""
- Sql = "select 单号,日期,货号,产品名称,数量,仓库 from [" & shnm & "$] where 日期 between #" & ks & "# And #" & js & "#"
- 'SQL = "select SQL from [" & shnm & "$] where 日期 between #" & ks & "# And #" & js & "#"
- [A5:F5000].Clear
- [A5].CopyFromRecordset cnn.Execute(Sql)
- cnn.Close
- Set cnn = Nothing
- '宏整理格式
- Columns("A:H").Select
- Range("A5").Activate
- With Selection
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- .WrapText = False
- .Orientation = 0
- .AddIndent = False
- .IndentLevel = 0
- .ShrinkToFit = False
- .ReadingOrder = xlContext
- End With
- Range("A1:H2").Select
- Columns("A:A").EntireColumn.AutoFit
- Columns("B:B").EntireColumn.AutoFit
- Columns("C:C").EntireColumn.AutoFit
- Columns("D:D").EntireColumn.AutoFit
- Columns("E:E").EntireColumn.AutoFit
- Columns("F:F").EntireColumn.AutoFit
- Columns("G:G").EntireColumn.AutoFit
- Columns("H:H").EntireColumn.AutoFit
- Range("L2:L3").Select
- End Sub
复制代码这里自己加上判断,但只能实现第一条……老师们帮我看一下错了那里?我一条条来测就可以 |
- If IsDate(ks) Or IsDate(js) Or kh > "" Or ck > "" Then
- '所有条件齐全查找;
- Sql = "select 单号,日期,货号,产品名称,数量,仓库 from [" & shnm & "$] where 日期 between #" & ks & "# And #" & js & "# and 货号=" & kh & "and 仓库='" & ck & "';"
-
- ElseIf IsDate(ks) Or IsDate(js) Or kh > "" Then
- '日期段和货号查找;
- Sql = "select 单号,日期,货号,产品名称,数量,仓库 from [" & shnm & "$] where 日期 between #" & ks & "# And #" & js & "# and 货号=" & kh & "';"
-
- ElseIf IsDate(ks) Or IsDate(js) Or ck > "" Then
- '仓库和时间段查找;
- Sql = "select 单号,日期,货号,产品名称,数量,仓库 from [" & shnm & "$] where 日期 between #" & ks & "# And #" & js & "# and 仓库='" & ck & "';"
- ElseIf IsDate(ks) Or IsDate(js) = "" Then
- '仓库和货号查询;
- Sql = "select 单号,日期,货号,产品名称,数量,仓库 from [" & shnm & "$] where 货号=" & kh & "and 仓库='" & ck & "';"
-
- ElseIf IsDate(ks) Or IsDate(js) Or kh = "" Then
- '所在仓库查询;
- Sql = "select 单号,日期,货号,产品名称,数量,仓库 from [" & shnm & "$] where 仓库='" & ck & "';"
- ElseIf IsDate(ks) Or IsDate(js) Or ck = "" Then
- '货号查询;
- Sql = "select 单号,日期,货号,产品名称,数量,仓库 from [" & shnm & "$] where 货号=" & kh & ""
-
- End If
复制代码
提问1.rar
(54.33 KB, 下载次数: 31)
|
|