(Solved) – VBA – Excel Macro – How to get exact match data from another sheet?

(solved)-–-vba-–-excel-macro-–-how-to-get-exact-match-data-from-another-sheet?

I have data in “Database” worksheet as shown below:

enter image description here

I have written a short code to get the data matching with certain criteria from “Database” sheet and paste it on another sheet “SelectedRecords”

Here is my code

Sub CopyData()
Dim db As Worksheet
Dim rcd As Worksheet

Set db = ThisWorkbook.Sheets("Database")
Set rcd = ThisWorkbook.Sheets("SelectedRecords")

db.Range("A1:C7").AdvancedFilter _
    Action:=xlFilterCopy, _
    CriteriaRange:=Range("$A$1:$A$2"), _
    CopyToRange:=rcd.Range("$A$4:$B$4")

End Sub

This code is working but I am getting some additional records as shown below:

enter image description here

I am looking for data where Name = “Jack” but in output I am getting all the data starting with “Jack”. How can I get records that are exactly matching criteria mentioned in A2 cell. In this case my output should only give one record for “Jack”.

Other two records “JackSparrow” and “Jackson” should be filtered out.

Leave a Reply

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