VLOOKUP in VBA

BBCC0000

New Member
Joined
Nov 2, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Dear all,

Thanks for reading this thread. My question is:

First, I will paste some Number in Sheet2 row A, may I know how to create a macro that can VLOOKUP their Code from Sheet1?
If the Number isn't available in Sheet1, can it paste all the missing Number in Sheet2 to the last row of Number of Sheet1?

Sheet1:
View attachment 103564

Sheet2 (Before running the macro):
1702633366303.png


Sheet2 (After running the macro):
1702633344469.png

The function looks like: =VLOOKUP(A2,Sheet1!A:B,2,0)

Is it any possible way to create a macro like this?

I appreciate your time to read this question. Thanks in advance.
 

Attachments

  • 1702633302625.png
    1702633302625.png
    14.3 KB · Views: 8

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Dear all,

Thanks for reading this thread. My question is:

First, I will paste some Number in Sheet2 row A, may I know how to create a macro that can VLOOKUP their Code from Sheet1?
If the Number isn't available in Sheet1, can it paste all the missing Number in Sheet2 to the last row of Number of Sheet1?

Sheet1:
1702634070428.png


Sheet2 (Before running the macro):
View attachment 103566

Sheet2 (After running the macro):
View attachment 103565
The function looks like: =VLOOKUP(A2,Sheet1!A:B,2,0)

Is it any possible way to create a macro like this?

I appreciate your time to read this question. Thanks in advance.
 
Upvote 0
Perhaps the below will help:
VBA Code:
Sub test()
    Dim rCell As Range
    
    For Each rCell In Sheet2.Range("A2:A" & Sheet2.Range("A" & Rows.Count).End(xlUp).Row)
        rCell.Offset(, 1) = Evaluate("VLOOKUP(" & rCell.Value & ",Sheet1!A:B,2,0)")
        If IsError(rCell.Offset(, 1)) Then
            Sheet1.Range("A" & Sheet1.Range("A" & Rows.Count).End(xlUp).Row + 1) = rCell.Value
        End If
    Next rCell
End Sub
 
Upvote 1
Another option:
VBA Code:
Sub Xlookup_Sht1()
    With Worksheets("Sheet2").Range("B2:B" & Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row)
        .Formula = "=XLOOKUP(A2,Sheet1!A:A,Sheet1!B:B,"""",0)"
        .Value = .Value
    End With
End Sub
 
Upvote 0
Try again...
VBA Code:
Sub Xlookup_Sht1()
    With Worksheets("Sheet2").Range("B2:B" & Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row)
        .Formula = "=XLOOKUP(A2,Sheet1!A:A,Sheet1!B:B)"
        On Error Resume Next
        Worksheets("Sheet2").Range("B2:B" & Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeFormulas, xlErrors).Offset(, -1).Copy Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        .Value = .Value
    End With
End Sub
 
Upvote 0
Without the duplicates
VBA Code:
Sub Xlookup_Sht1()
    With Worksheets("Sheet2").Range("B2:B" & Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row)
        .Formula = "=XLOOKUP(A2,Sheet1!A:A,Sheet1!B:B)"
        On Error Resume Next
        Worksheets("Sheet2").Range("B2:B" & Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeFormulas, xlErrors).Offset(, -1).Copy Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        .Value = .Value
    End With
    Worksheets("Sheet1").Range("A:B").RemoveDuplicates Columns:=1, Header:=xlYes
End Sub
 
Upvote 0
Perhaps the below will help:
VBA Code:
 Sub test() Dim rCell As Range For Each rCell In Sheet2.Range("A2:A" & Sheet2.Range("A" & Rows.Count).End(xlUp).Row) rCell.Offset(, 1) = Evaluate("VLOOKUP(" & rCell.Value & ",Sheet1!A:B,2,0)") If IsError(rCell.Offset(, 1)) Then Sheet1.Range("A" & Sheet1.Range("A" & Rows.Count).End(xlUp).Row + 1) = rCell.Value End If Next rCell End Sub
 
Upvote 0
Thank you everyone for your kind help. Hope you guys have a nice day!!
 
Upvote 0

Forum statistics

Threads
1,215,101
Messages
6,123,087
Members
449,095
Latest member
gwguy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top