查找范围内的列号?

我正在尝试使用filter选项来过滤我的范围。

ActiveSheet.Range("$K$2:$ZZ$200").AutoFilter Field:=11, Criteria1:="yes" 

基本上这将过滤列U是的。
我所拥有的是一个下拉列表,我需要它来查找该条目在哪个字段中。 例如,列U包含名称“John”

所以如果我从下拉列表中selectJohn,就需要查看范围,findJohn列,然后返回它的字段(在本例中是11)

T列是Ben的名字。 如果我从下拉列表中selectBen,那么它将执行相同的filter,但是该字段将是10。

无论如何,我可以根据从下拉菜单中select的内容来计算字段编号吗?

  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 

所以这就是我如何工作。 现在我只需要知道如何将下拉菜单中的选项作为variables存储,并设置一个macros来清除filter和一个去! 聚苯乙烯 – 无法得到最后一列查找位工作,因为它一直说“对象需要”,所以手动设置它

您可以使用acell的当前区域属性来获取区域内的列号,请参阅此处

 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 

根据需要清除comboboxselect并显示所有数据