简体   繁体   English

Excel VBA 查找文本并返回行号(循环)

[英]Excel VBA finding a text and return row number (loop)

I am finding a difficulty in probably a rather simple code to return the corresponding row numbers which contain a specific text in column("A:A").我发现一个可能相当简单的代码很难返回包含列中特定文本的相应行号(“A:A”)。 Have been trying several ways but without success - it does not loop through the last row.一直在尝试几种方法但没有成功 - 它不会遍历最后一行。 Here is what I have got so far:这是我到目前为止所得到的:

Dim rowCount As Integer
Dim i As Integer
Dim FindRow As Variant
Dim RowN As Integer
Dim blockSize As Integer

rowCount = Range("A1").CurrentRegion.Rows.Count
 
For i = 2 To rowCount 
    
    Set FindRow = Cells(i, 1).Find(What:="group: 1", LookAt:=xlPart, SearchOrder:=xlByRows)
    RowN = FindRow.Row
    MsgBox RowN

    If RowN > 1 Then
    blockSize = FindRow.Row - 1
    MsgBox blockSize
    End If

Next i

Your Find is only searching one cell in the region.您的Find仅搜索该区域中的一个单元格。 Using that method you should use Instr to check each cell.使用该方法,您应该使用Instr检查每个单元格。

The Find method will find the first occurrence in a range, and FindNext will find subsequent occurrences. Find方法将查找范围内的第一个匹配项, FindNext将查找后续匹配项。

Option Explicit
    
Sub Test()

    Dim lastrow As Long
    
    With ThisWorkbook.Worksheets("Sheet1")
        lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
        
        Dim srchRange As Range
        Set srchRange = .Range(.Cells(1, 1), .Cells(lastrow, 1))
    End With
    
    With srchRange
        Dim rFound As Range
        Set rFound = .Find("group: 1", .Cells(1, 1), xlValues, xlPart, , xlNext, False)
        If Not rFound Is Nothing Then
            Dim firstAdd As String
            firstAdd = rFound.Address
            
            Dim FoundRows As String
            Dim blocksize As Long
                
            Do
                If rFound.Row > 1 Then
                    blocksize = rFound.Row - 1
                    'other code.
                    
                End If
                Set rFound = .FindNext(rFound)
            Loop Until rFound.Address = firstAdd
        
        End If
    End With

End Sub

If you wanted to add more flexibility to the process you could rewrite it as a function so you can search different groups, different columns and different sheets.如果您想为流程增加更多灵活性,您可以将其重写为 function,以便您可以搜索不同的组、不同的列和不同的工作表。

Sub Test1()

    Dim Result As Variant
    Result = GetBlocks(, , ThisWorkbook.Worksheets("Sheet1"))
    
    If IsEmpty(Result) Then
        MsgBox "No groups found."
    Else
        Dim itm As Variant
        For Each itm In Result
            MsgBox itm
        Next itm
    End If

End Sub

Function GetBlocks(Optional GroupID As String = "group: 1", _
                   Optional ColNum As Long = 1, _
                   Optional wrkSht As Worksheet) As Variant
                   
    'Optional arguments must be constant expressions, so a
    'default worksheet can't be set before here.
    If wrkSht Is Nothing Then Set wrkSht = ActiveSheet

    'Define the range to be searched.
    With wrkSht
        Dim lastrow As Long
        lastrow = .Cells(.Rows.Count, ColNum).End(xlUp).Row
        
        Dim srchRange As Range
        Set srchRange = .Range(.Cells(1, ColNum), .Cells(lastrow, ColNum))
    End With
    
    With srchRange
        Dim rFound As Range
        Set rFound = .Find(GroupID, .Cells(1, 1), xlValues, xlPart, , xlNext, False)
        If Not rFound Is Nothing Then
            Dim firstAdd As String
            firstAdd = rFound.Address
            
            'Create a string of row numbers.
            'e.g. 4,6,8,11,13,14,16,17, < note final comma.
            Dim FoundRows As String
            Do
                If rFound.Row > 1 Then
                    FoundRows = FoundRows & rFound.Row & ","
                End If
                Set rFound = .FindNext(rFound)
            Loop Until rFound.Address = firstAdd
            
            'Split string into arrow of row numbers.
            'These will be string data types.
            Dim tmp As Variant
            tmp = Split(FoundRows, ",")
            
            'Convert string to long data type.
            ReDim tmp1(0 To UBound(tmp) - 1)
            Dim x As Long
            For x = 0 To UBound(tmp1)
                tmp1(x) = CLng(tmp(x))
            Next x
            
            'Return result of function.
            GetBlocks = tmp1
            
        End If
    End With

End Function

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

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