Search Multiple Values in Worksheet Data

How to search multiple values in worksheet data using VBA. Earlier we had learnt how to search for data in a worksheet using a looping process but were able to find a single value. Today we learn to extract all the occurrences of data in our worksheet like an item code with VBA. Watch the video below:

Watch this video on YouTube.

Here’s the complete VBA code to find multiple data withe the same value:

Sub searchMultipleValues()
Dim erow As Long
Dim ws As Worksheet
Dim lastrow As Long
Dim count As Integer

lastrow = Sheets(“item_price”).Cells(Rows.count, 1).End(xlUp).Row
Sheet2.Range(“a11:C1000”).ClearContents

count = 0

Dim p As Long

p = 11

For x = 2 To lastrow

If Sheets(“item_price”).Cells(x, 1) = Sheet2.Range(“b3”) Then
Sheet2.Cells(p, 1) = Sheets(“item_price”).Cells(x, 1)
Sheet2.Cells(p, 2) = Sheets(“item_price”).Cells(x, 2)
Sheet2.Cells(p, 3) = Sheets(“item_price”).Cells(x, 3)
p = p + 1
count = count + 1
End If
Next x

MsgBox ” The number of data found for this item code is ” & ” ” & count

If count = 0 Then
Set ws = Worksheets(“Sheet3”)
With ws
erow = .Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
.Cells(erow, 1) = Date
.Cells(erow, 2) = Sheet2.Range(“B3”)
End With
End If

Sheet2.Range(“A10”).Select

End Sub