Monday, December 22, 2014

Excel VBA - Search Match Text

Following subroutines can be used to search a word "pattern" in column B and assume total row is 50.

This search from top:

Sub topDownSearch()
Dim lastRow As Integer
Dim curRow As Integer

lastRow = 50

For curRow = 1 to lastRow Step 1
    If Cells(curRow, 2) = "pattern" Then
        Cells(curRow, 2).Select
        MsgBox (Cells(curRow, 2))
     End If
Next

End Sub

This search from bottom:

Sub bottomUpSearch()
Dim lastRow As Integer
Dim curRow As Integer

lastRow = 50

For curRow = lastRow To 1 Step -1
    If Cells(curRow, 2) = "pattern" Then
        Cells(curRow, 2).Select
        MsgBox (Cells(curRow, 2))
    End If
Next

End Sub




No comments: