January 21, 2016

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.

3 thoughts on “Search Data with VBA

  1. sir i want to take a coaching in South Delhi can you tell me a best institute in South Delhi to Learn VBA, MAcro and SAP

  2. 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.

Comments are closed.