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: 10
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

Hi,

I found out that when I'm using this code, if there are two consecutive numbers in Sheet2 that it cannot be found in Sheet1, it will have error, stating "Object required".

After I added "On Error Resume Next", it worked well but it doesn't paste all the missing Number in Sheet2 to the last row of Number of Sheet1...

Could you please help me on this?
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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

When I'm using this, after I run the macro, it will paste all the missing Number in Sheet2 to the last row of Number of Sheet1, but in Sheet2 all the value will become "#NAME?"
 
Upvote 0
Usually, the #NAME? error occurs when Excel doesn't recognise a function you're trying to use. Your profile says you have 365 so I assumed the XLOOKUP() function was available to you. Try typing into a cell =XLOO and see if Excel suggests XLOOKUP for you?

Anyhow, try the following instead using the trusty old Vlookup instead:
VBA Code:
Sub Vlookup_Sht1()
    With Worksheets("Sheet2").Range("B2:B" & Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row)
        .Formula = "=VLOOKUP(A2,Sheet1!A:B,2,FALSE)"
        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 1
Solution
Usually, the #NAME? error occurs when Excel doesn't recognise a function you're trying to use. Your profile says you have 365 so I assumed the XLOOKUP() function was available to you. Try typing into a cell =XLOO and see if Excel suggests XLOOKUP for you?

Anyhow, try the following instead using the trusty old Vlookup instead:
VBA Code:
Sub Vlookup_Sht1()
    With Worksheets("Sheet2").Range("B2:B" & Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row)
        .Formula = "=VLOOKUP(A2,Sheet1!A:B,2,FALSE)"
        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

Yes and sorry, my pc doesn't recognize XLOOKUP() function... And thanks again! It works well :D
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,863
Members
452,948
Latest member
UsmanAli786

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