+ Reply to Thread
Results 1 to 2 of 2

Thread: Recurrent Match Formula to Locate Multiple Instance of a String and Return Row Number

  1. #1
    Registered User
    Join Date
    01-25-2012
    Location
    Nashville, Tennessee
    MS-Off Ver
    Excel 2007
    Posts
    2

    Recurrent Match Formula to Locate Multiple Instance of a String and Return Row Number

    I need to write a macro that will return the column number of n instances of a string. The idea is to find the first instance of a particular string, return its row number and modify something using that row number, then using a for next loop use that row number as the new starting point to find the next instance, etc.

    Heres the code I have so far:
    Sub Find()
    Dim RowCount As Integer
    
    RowCount = 0
    
    For i = 1 To 5
    RowCount = WorksheetFunction.Match("Hello", Range("A" & RowCount + 1 & ":A100"), 0)
    Range("B" & RowCount) = "World"
    Next i
    
    End Sub

    In this example I am finding every instance of Hello in Column A, the using the row number to modify the exact same row in Column B to say World. Then the for loop increments and the Match function starts from the row just below the previous instance of Hello.

    I just can't seem to get this to work right.

    Any help is greatly appreciated.

  2. #2
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    448

    Re: Recurrent Match Formula to Locate Multiple Instance of a String and Return Row Nu

    suppose data like this from A1 down

    1
    2
    3
    hello
    4
    5
    hello
    6
    7
    8
    hello
    9
    8
    7

    try this macro

    Sub test()
    Dim r As Range, cfind As Range, j As Long, add As String
    Set r = Range(Range("A1"), Range("a1").End(xlDown))
    Set cfind = r.Cells.Find(what:="hello", lookat:=xlWhole)
    If Not cfind Is Nothing Then
    add = cfind.Address
    j = cfind.Row
    MsgBox j
    Cells(cfind.Row, "B") = "world"
    Else
    MsgBox "that value is not available"
    Exit Sub
    End If
    
    Do
    Set cfind = Cells.FindNext(cfind)
    If cfind Is Nothing Then Exit Do
    If cfind.Address = add Then Exit Do
    j = cfind.Row
    MsgBox j
    
    Cells(cfind.Row, "B") = "world"
    
    Loop
    
    End Sub
    I am not an expert. better solutions may be available
    $$$$venkat1926$$$$@gmail.com

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0