我想基于下拉列表选择过滤表C列。 我有更多行,我可以从下拉列表中选择国家/地区代码。
我想根据国家/地区代码选择过滤我的表格。
例如:
第一行:选择“54”国家/地区代码
选择第二行“24”国家/地区代码
依旧......
其他标签上的表格将按所选国家/地区代码“54”,“24”进行过滤。
您能帮我解决一下如何管理它?
谢谢:)
答案 0 :(得分:0)
Sub FilterRangeCriteria()
Dim vCrit As Variant
Dim wsFiltered As Worksheet
Dim wsSelection As Worksheet
Dim rngCrit As Range
Dim rngOrders As Range
Dim Lastrow As Integer
'you need more variables to save the range in an array
Dim valArr As Variant
Dim cl As Range
Dim i As Integer
Set wsFiltered = Worksheets("S") ' I want to filter this tab with "Centre Information" selection
Set wsSelection = Worksheets("Centre Information")
Set rngOrders = wsFiltered.Range("b:b") 'I want to filter this column
Lastrow = Worksheets("Centre Information").Cells(Rows.Count, 2).End(xlUp).Row
myrange = ("b3:b" & Lastrow) ' the value from B3 until last row: this will be the filter data
Set rngCrit = wsSelection.Range(myrange)
vCrit = rngCrit.Value
'I get error here: Autofilter method of range class failed
'Correction: Fill array
ReDim valArr(Lastrow - 3) 'define array size (first two rows are empty + considering the first array position starts with 0)
i = 0
For Each cl In rngCrit 'loop through range
valArr(i) = "=" & cl 'filter for each value + operator
i = i + 1
Next cl
'Correction: use array als range of numbers which shall be matched
rngOrders.AutoFilter _
Field:=1, _
Criteria1:=valArr, _
Operator:=xlFilterValues
End Sub
注释:如果要过滤范围,请使用自动过滤器提供一个包含字符串格式的所有值的数组。过滤数字需要操作员:例如“=”,“< =”,等等。祝贺。