我正在尝试使用过滤器选项来过滤我的范围。
ActiveSheet.Range("$K$2:$ZZ$200").AutoFilter Field:=11, Criteria1:="yes"
基本上会将列U过滤为是。
我所拥有的是一个下拉列表,我需要它来查找该范围的条目所在的字段。例如,列U包含名称“John”
因此,如果我从下拉列表中选择John,则需要查看范围,找到John列,然后返回它所在的字段(在本例中为11)
在T栏中是Ben的名字。如果我从下拉列表中选择Ben,那么它将执行相同的过滤器,但字段将为10.
无论如何,我可以根据从下拉列表中选择的内容来计算字段数吗?
Sub Report()
Dim oSht As Worksheet
Dim lastRow As Long
Dim LastCol As Long
Dim nice As Long
Dim strSearch As String
Dim aCell As Range
Set oSht = Sheets("Overview")
lastRow = oSht.Range("B" & Rows.Count).End(xlUp).Row
With ActiveSheet.Shapes("Dropdown").ControlFormat
strSearch = .List(.Value)
End With
MsgBox strSearch
Set aCell = oSht.Range("K2:Z100").Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
nice = aCell.Column - 10
ActiveSheet.Range("$K$2:$ZZ$200").AutoFilter Field:=nice, Criteria1:="yes"
End If
End Sub
所以这就是我的工作方式。现在我只需要知道如何将下拉列表中的选项存储为变量并设置一个宏来清除过滤器,它就行了! PS - 无法让最后一列找到工作,因为它一直说“需要对象”,所以手动设置它
答案 0 :(得分:0)
您可以使用acell的当前region属性来获取区域内的列号,请参阅here
Sub Report()
Dim oSht As Worksheet
Dim lastRow As Long
Dim LastCol As Long
Dim nice As Long
Dim strSearch As String
Dim aCell As Range
Set oSht = Sheets("Overview")
lastRow = oSht.Range("B" & Rows.Count).End(xlUp).Row
With ActiveSheet.Shapes("Dropdown").ControlFormat
strSearch = .List(.Value)
End With
MsgBox strSearch
Set aCell = oSht.Range("K2:Z100").Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
'nice = aCell.Column - 10
'ActiveSheet.Range("$K$2:$ZZ$200").AutoFilter Field:=nice, Criteria1:="yes"
''''''
With aCell
nice = aCell.Column - .CurrentRegion.Cells(1).Column + 1
ActiveSheet.Range("$K$2:$ZZ$200").AutoFilter Field:=nice,Criteria1:="yes" 'Criteria1:=aCell
End With
End If
End Sub
然后使用
的组合Sub ClearCombo()
With Sheets("Sheet1").ComboBox1
.Clear
End
Sub showAllData()
Worksheets("Sheet1").ShowAllData
End Sub
根据需要清除组合框选择并显示所有数据