I made a simple loop to go trough worksheet and to print multiple hits for .Find in a Range
Right now my code displays all cells containing any data in it.
What I would like is:
when there is a HIT to continue with .FindNext but to skip to next row
This is my sample table:
NAME |
VALUE 1 |
VALUE 2 |
VALUE 3 |
WORKER 1 |
somethinW1.1 |
somethingW1.2 |
|
WORKER 2 |
|
somethingW2.2 |
somethingW2.3 |
WORKER 3 |
somethingW3.1 |
|
somethingW3.3 |
WORKER 4 |
|
|
|
WORKER 5 |
|
somethingW5.2 |
|
This is my VBA code:
Sub sampleSearch()
Dim wsh as Long
wsh = 5
Dim lastRow As Long
lastRow = Worksheets(wsh).Cells(Rows.Count, 1).End(xlUp).Row
Dim name As String: name = "*"
Dim rgSearch As Range
Set rgSearch = Worksheets(wsh).Range("B1:D" & lastRow)
Dim cell As Range
Set cell = rgSearch.Find(name)
If cell Is Nothing Then
Debug.Print "Not found"
GoTo NothingFound
End If
' Store first cell address
Dim firstCellAddress As String
firstCellAddress = cell.Address
' Find all cells containing NAME
Do
If cell = "NAME" Or cell = "VALUE 1" Or cell = "VALUE 2" Or cell = "VALUE 3" Then GoTo SkipToNextFound
Debug.Print "Found: " & cell.Address(False, False)
Debug.Print Worksheets(wsh).Range("A" & cell.Row) & cell.Row
SkipToNextFound:
Set cell = rgSearch.FindNext(cell)
Loop While firstCellAddress <> cell.Address
NothingFound:
Debug.Print Worksheets(wsh).name & " " & lastRow & " " & rgSearch.Address(False, False) & vbCr & "END"
End Sub
My Immediate window displays result like this:
Found: B2
WORKER 1 2
Found: C2
WORKER 1 2
Found: C3
WORKER 2 3
Found: D3
WORKER 2 3
Found: B4
WORKER 3 4
Found: D4
WORKER 3 4
Found: C6
WORKER 5 6
I need somethin like this:
Found: B2
WORKER 1 2
Found: C3
WORKER 2 3
Found: B4
WORKER 3 4
Found: C6
WORKER 5 6
Bookmarks