|
- Sub Filter()
- Dim cnn As Object, rst As Object
- Dim strPath As String, strCnn As String, strSQL As String
- Set cnn = CreateObject("adodb.connection")
- Dim i As Long
- strPath = ThisWorkbook.FullName
- strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & strPath
- cnn.Open strCnn
-
- Dim aTj1, aTj2, aStrTj1, aStrTj2
- aTj1 = Range([a8], Range("a8").End(xlDown))
- aTj2 = Range([b8], Range("b8").End(xlDown))
- For i = 1 To UBound(aTj1)
- aStrTj1 = aStrTj1 & "'" & aTj1(i, 1) & "',"
- Next
- aStrTj1 = Mid(aStrTj1, 1, Len(aStrTj1) - 1)
-
- For i = 1 To UBound(aTj2)
- aStrTj2 = aStrTj2 & "'" & aTj2(i, 1) & "',"
- Next
- aStrTj2 = Mid(aStrTj2, 1, Len(aStrTj2) - 1)
-
-
- ' strSQL =SELECT * FROM [设$a20:f]
- ' where 日期 BETWEEN #2022-8-5# and #2022-8-25#
- ' AND 数量 BETWEEN 12 and 40
- ' AND 项目 in("打印机","空调")
- ' AND 销售员 in("张三","李四","王五","周七","胡八")
- strSQL = "SELECT * FROM [设$a20:f]" _
- & " where 日期 BETWEEN #" & Range("a4") & "# and #" & Range("a5") & "#" _
- & " AND 数量 BETWEEN " & Range("b4") & " and " & Range("b5") _
- & " AND 项目 in (" & aStrTj1 & ")" _
- & " AND 销售员 in(" & aStrTj2 & ")"
- Set rst = cnn.Execute(strSQL)
-
- Application.DisplayAlerts = False
- On Error Resume Next
- Sheets("新").Delete
- Sheets.Add(after:=Sheets("设")).Name = "新"
- With Sheets("新")
- For i = 0 To rst.Fields.Count - 1
- .Cells(1, i + 1) = rst.Fields(i).Name
- Next
- .Range("a:a").NumberFormatLocal = "yyyy-m-d"
- .Range("a2").CopyFromRecordset rst
- .Range("a2").CurrentRegion.Borders.LineStyle = xlContinuous
- .Range("a2").CurrentRegion.EntireColumn.AutoFit
- End With
- Application.DisplayAlerts = True
-
- rst.Close
- cnn.Close
- Set rst = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|