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.

Published by

Dinesh Kumar Takyar

Welcome to! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: For a structured Excel VBA training course online you can visit:

3 thoughts on “Search Data with VBA”

  1. This is really awesome and very much useful. thank you. I need your help on my below request.
    We have data like this,

    S. no Par number Document number Created Date Reference document

    here for single part number there are so many document numbers available also created date and reference document. My request is if search for one part number it should shows wherever this part occurs also relevant document number,created date and reference document. how can I do this with Macros. please help me.

    Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *