繁体   English   中英

在一个范围内查找列号?

[英]Look up Column number in a range?

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

 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 - 无法让最后一列找到工作,因为它一直说“需要对象”,所以请手动设置它

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

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

根据需要清除组合框选择并显示所有数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM