Skip to content

Instantly share code, notes, and snippets.

@scorchio
Created April 23, 2017 21:44
Show Gist options
  • Save scorchio/c6a3f69befb442081988b7e2e0a43ec9 to your computer and use it in GitHub Desktop.
Save scorchio/c6a3f69befb442081988b7e2e0a43ec9 to your computer and use it in GitHub Desktop.
"Looping over" values in Excel VBA with Find()/FindNext()
Dim matchRange As Range, firstMatchRange As Range
Set matchRange = ws.columns(COLUMN_TO_SEARCH_IN).Find( _
What:=VALUE_TO_SEARCH_BY, LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchByte:=False)
' Don't forget that the match will be the cell only, you will need
' to get the entire row from that for usual processing.
' Check if there's a match at all
If Not matchRange Is Nothing Then
' Memorize first match to be able to recognize whether we've got
' back to start
Set firstMatchRange = matchRange
Do
' ... DO PROCESSING ON THE MATCH...
' ...and jump to the next one
Set matchRange = _
ws.columns(COLUMN_TO_SEARCH_IN).FindNext(matchRange)
Loop While Not matchRange Is Nothing And _
matchRange.Address <> firstMatchRange.Address
End If
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment