【VBA案例008】多条件查询

  1. 方法一:
  2. 方法二:

大家好!今天分享的案例是多条件查询。

这个查询在进销存或者库存管理中特别常用,如果你准备或者正在做一个自己的管理查询工具,这个方法一定要会。先来看一下数据。

比如说,现在有一份产品信息表。

我们要做的是在查询页面,输入参数后,查询出所有满足条件的内容。其中参数可以不填,不填就表示要查询所有内容。

本期将使用2个方法来实现效果,以下是VBA代码,详细视频解析在文末。

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Sub 方法一()

Dim i, j, k
Dim ar, br()

With Sheet2
ar = .Range("a1:f" & .[a65536].End(3).Row)
End With

ReDim br(1 To UBound(ar), 1 To UBound(ar, 2))

Dim customer, product, startDate As Date, endDate As Date
With Sheet1
customer = IIf(.[b2] = "", "", "," & .[b2] & ",")
product = IIf(.[d2] = "", "", "," & .[d2] & ",")
startDate = IIf(.[f2] = "", #1/1/1900#, .[f2])
endDate = IIf(.[h2] = "", #1/1/2400#, .[h2])
End With

For i = 2 To UBound(ar)
If ar(i, 1) >= startDate And ar(i, 1) <= endDate Then
If InStr("," & ar(i, 2) & ",", customer) > 0 Then
If InStr("," & ar(i, 3) & ",", product) > 0 Then
k = k + 1
For j = 1 To UBound(br, 2)
br(k, j) = ar(i, j)
Next j
End If
End If
End If
Next i

Sheet1.[a5:f65536].ClearContents
If k > 0 Then
Sheet1.[a5].Resize(k, UBound(br, 2)) = br
End If

End Sub

方法二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Sub SQL查询()
'定义变量
Dim cnn, rst, SQL$
Dim i, j, k
Set cnn = CreateObject("adodb.connection") '创建数据库连接
Set rst = CreateObject("adodb.recordset") '创建一个数据集保存数据

'设置数据库连接
If Val(Application.Version) < 12 Then
cnn.Open "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties='Excel 8.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
Else
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
End If

Dim customer, product, startDate As Date, endDate As Date
With Sheet1
customer = IIf(.[b2] = "", "", "," & .[b2] & ",")
product = IIf(.[d2] = "", "", "," & .[d2] & ",")
startDate = IIf(.[f2] = "", #1/1/1900#, .[f2])
endDate = IIf(.[h2] = "", #1/1/2400#, .[h2])
End With

'设置SQL语句
SQL = "select * from [产品信息$A1:F22] where 日期>=#" & startDate & "# and 日期<=#" & endDate & "# and instr(','&客户&',','" & customer & "')>0 and instr(','&产品&',','" & product & "')>0" '

'SQL结果处理
Set rst = cnn.Execute(SQL)

Sheet1.[a5:f65536].ClearContents
Sheet1.Range("a5").CopyFromRecordset rst
' For i = 1 To rst.Fields.Count
' Cells(1, i + 5) = rst.Fields(i - 1).Name
' Next

rst.Close
cnn.Close '关闭数据库连接
Set rst = Nothing
Set cnn = Nothing '将cnn从内存中删除
End Sub

原始链接


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 richffan@outlook.com

文章标题:【VBA案例008】多条件查询

字数:682

本文作者:Rich Fan

发布时间:2023-10-23, 00:00:00

最后更新:2024-02-27, 08:17:39

原始链接:http://fanrich.github.io/2023/10/22/VBA/VBA%E6%A1%88%E5%88%97/%E3%80%90VBA%E6%A1%88%E4%BE%8B008%E3%80%91%E5%A4%9A%E6%9D%A1%E4%BB%B6%E6%9F%A5%E8%AF%A2/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。