我的数据表中有多列,其中3列是我过滤数据的首选字段。
这是工作表名称为“数据”的数据表
这是另一张名为“ Analysis”的工作表中的代码,用户将输入其首选的日期,行和市场以过滤“数据”工作表中的数据表
Dim lngStart As Date, lngEnd As Date
Dim LineNum As Integer
Dim MarketDesc As String
lngStart = Range("B6").value
lngEnd = Range("B7").value
LineNum = Range("D7").value
MarketDesc = Range("D8").value
With Sheets("Data").Range("B6:Z150")
.AutoFilter Field:=2, _
Criteria1:=">=" & lngStart, _
operator:=xlAnd, _
Criteria2:="<=" & lngEnd
.AutoFilter Field:=4, _
Criteria1:="=" & LineNum
.AutoFilter Field:=5, _
Criteria1:="=" & MarketDesc
End With
错误是如果该列具有与Market中不同的数据。它包含本地和导出的位置。突然不过滤我的首选数据所在的表
行:2,市场:本地,类别:速溶妈妈,变体:鸡肉。
输出应该像上面的示例一样显示数据,但是在图片中显示了Market:本地和导出
12/1/2018
作为开始日期12/7/2018
作为结束日期2
按行Export
作为市场该表格将根据首选项进行过滤
答案 0 :(得分:0)
您正在使用.Range(B6:Z150)
任何一个
1
,3
和4
.Range("A6:Z150")
也就是说,如果您打算过滤列B
,D
和E
答案 1 :(得分:0)
我能够使用以下代码过滤数据并检查过滤条件:
Option Explicit
Sub SetFilter()
'Dim lngStart As Date, lngEnd As Date
Dim lngStart As Long, lngEnd As Long
Dim LineNum As Integer
Dim MarketDesc As String
lngStart = DateSerial(2018, 12, 1) 'Range("B6").Value
lngEnd = DateSerial(2018, 12, 15) 'Range("B7").Value
LineNum = 2 'Range("D7").Value
MarketDesc = "Export" 'Range("D8").Value
With Sheets("Data").Range("A5:G170")
.AutoFilter Field:=2, _
Criteria1:=">=" & lngStart, _
Operator:=xlAnd, _
Criteria2:="<=" & lngEnd
.AutoFilter Field:=4, _
Criteria1:="=" & LineNum
.AutoFilter Field:=5, _
Criteria1:="=" & MarketDesc
End With
End Sub
Sub ShowFilters()
Dim ws As Worksheet, flt As Filter, i As Integer
Dim operatorString As String
Set ws = ActiveSheet
' If there are filters
If ws.AutoFilterMode Then
' Get each Filter object
For i = 1 To ws.AutoFilter.Filters.Count
Set flt = ws.AutoFilter.Filters(i)
' And if the filter is on, show its criterion.
If flt.On Then
On Error Resume Next
If flt.Operator Then
'search for Autofilter VBA Operator parameters and also for the xlFilterDynamic operator
operatorString = Operator2String(flt.Operator)
Debug.Print "Column " & i & ": " & flt.Criteria1 & operatorString & flt.Criteria2
Else
Debug.Print "Column " & i & ": " & flt.Criteria1
End If
On Error GoTo 0
End If
Next
End If
End Sub
Function Operator2String(OperatorInt As Integer) As String
Select Case OperatorInt
Case 1 '1 xlAnd
Operator2String = " and "
Case 2 '2 xlOr
Operator2String = " or "
Case 3 '3 xlTop10Items
Operator2String = " NOT-TESTED: xlTop10Items "
Case 4 '4 xlBottom10Items
Operator2String = " NOT-TESTED: xlBottom10Items "
Case 5 '5 xlTop10Percent
Operator2String = " NOT-TESTED: xlTop10Percent "
Case 6 '6 xlBottom10Percent
Operator2String = " NOT-TESTED: xlBottom10Percent "
Case 7 '7 xlFilterValues
Operator2String = " NOT-TESTED: xlFilterValues "
Case 8 '8 xlFilterCellColor
Operator2String = " NOT-TESTED: xlFilterCellColor "
Case 9 '9 xlFilterFontColor
Operator2String = " NOT-TESTED: xlFilterFontColor "
Case 10 '10 xlFilterIcon
Operator2String = " NOT-TESTED: xlFilterIcon "
Case 11 '11 xlFilterDynamic
Operator2String = " NOT-TESTED: xlFilterDynamic (REVIEW subparameters) "
Case Else
Operator2String = " NOT-DEFINED "
End Select
End Function