【问题标题】:Find a string in a column and return and return an array with row numbers在一列中查找一个字符串并返回并返回一个带有行号的数组
【发布时间】:2020-07-08 20:07:35
【问题描述】:

我想在一个列中搜索并找到一个字符串。但是,该字符串有几个单元格,我想返回一个包含所有行位置的数组。

Dim r As Range
Set r = Sheets("Sheet3").columns(3).Find(What:="TEST", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

这仅返回第一行,但不是全部。 如何返回包含“TEST”的所有行? 谢谢。

【问题讨论】:

  • 这是最终的输出,还是您最终会使用这些引用返回其他内容?

标签: excel vba


【解决方案1】:

这应该可以解决问题...

它将循环遍历Sheet3 中的Column 3,以生成一个包含TEXT 每次出现的行号的数组。根据您示例中使用的选项,它区分大小写,并且必须占据整个单元格。

Sub demo_FindIntoArray()
    Const searchFor = "TEST" 'case sensitive whole-cell search term
    Const wsName = "Sheet3" 'worksheet name to search
    Const colNum = 3 'column# to search
    Dim r As Range, firstAddress As String, strTxt As String, arrRows
    With Sheets("Sheet3").Columns(3)
        Set r = .Find(searchFor, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not r Is Nothing Then
            firstAddress = r.Address
            Do
                If strTxt <> "" Then strTxt = strTxt & ","
                strTxt = strTxt & r.Row
                Set r = .FindNext(r)
            Loop While Not r Is Nothing And r.Address <> firstAddress
        End If
    End With

    If strTxt <> "" Then
        arrRows = Split(strTxt,",")
        MsgBox "Found " & UBound(arrRows)+1 & " occurrences of '" & searchFor & "':" & vbLf & vbLf & strTxt
    Else
        MsgBox "'" & searchFor & "' was not found."
    End If

    '[arrRows] is now an array containing row numbers

End Sub

它首先用逗号分隔的值列表构建一个字符串,然后使用Split function 将其拆分为一个数组。

【讨论】:

    【解决方案2】:

    这将联合等于“测试”的范围,

    Sub SelectA1()
        Dim FrstRng As Range
        Dim UnionRng As Range
        Dim c As Range
        Set FrstRng = Range("C:C").SpecialCells(xlCellTypeConstants, 23)
    
        For Each c In FrstRng.Cells
            If LCase(c) = "test" Then
                If Not UnionRng Is Nothing Then
                    Set UnionRng = Union(UnionRng, c)    'adds to the range
                Else
                    Set UnionRng = c
                End If
            End If
        Next c
    
        UnionRng.Select    ' or whatever you want to do with it.
    End Sub
    

    【讨论】:

      【解决方案3】:

      您没有指明要对结果做什么。但是,您可以使用工作表公式返回包含单词“TEST”的行号数组:

      不区分大小写:

      =AGGREGATE(15,6,SEARCH("TEST",$C:$C)*ROW($C:$C),ROW(INDIRECT("1:" & COUNTIF($C:$C,"*TEST*"))))
      

      区分大小写:

      =AGGREGATE(15,6,FIND("TEST",$C:$C)*ROW($C:$C),ROW(INDIRECT("1:" & SUMPRODUCT(--ISNUMBER(FIND("TEST",$C:$C))))))
      

      不区分大小写;单元格 = 测试(例如整个单元格内容):

      =AGGREGATE(15,6,1/($C:$C="test")*ROW($C:$C),ROW(INDIRECT("1:"&COUNTIF($C:$C,"test"))))
      

      如果您想将此数组用于其他用途,例如返回第三列 = "test" 的行的内容,您也可以使用 VBA 或工作表中的过滤器来执行此操作。

      有许多不同的适当解决方案,具体取决于您要如何处理这些行号的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多