Search Data with VBA

We can search data in Sheet1 (Data) and display the results in Sheet2 (Results) based on multiple conditions using VBA. Watch the training video below before studying the VBA macro code carefully. Also note that you cannot just copy this code and use it in your workbook because some characters especially quotes can change.

Watch this video on YouTube.

VBA code:

Sub searchdata()
Dim lastrow As Long, x As Long
lastrow = Sheets(“Data”).Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastrow
If Sheets(“Data”).Cells(x, 1).Value = Sheets(“Results”).Range
(“A2”).Value And Sheets(“Data”).Cells(x, 2).Value = Sheets
(“Results”).Range(“b2”).Value Then
Sheets(“Results”).Range(“c2”).Value = Sheets(“Data”).Cells(x, 3).Value
Exit Sub
End If
Next x
End Sub
Note how the worksheets are accessed using their names.