VBA VLOOKUP Help with dynamic range

newExcelSheet

New Member
Joined
Apr 4, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm trying to create a function in VBA using Vlookup. Essentially, within the same sheet I will have two columns (these will be pasted in by the user which is why they will be dynamic). I want to perform a Vlookup to see if any of the values in the first column can be found in the second column. I created the script below but the issue that I'm having is that in the results column its only showing the match for the first value instead of looking for the matches of all the values in the first column. I suspect there is an issue with the firstRange variable that I am passing into the Vlookup but I'm not sure whats causing the issue. Also the values will start at the 9th cell because I plan on having text above. I have attached an image as well showing how my sheet is set up.

Excel Formula:
Sub vlookup()
    Dim firstRange As Range, secondRange As Range
    Dim lastCell As Integer
    
    lastCell = Range("A9").End(xlDown).Row

    Set firstRange = Range(Range("A9"), Range("A9").End(xlDown))
    Set secondRange = Range(Range("C9"), Range("C9").End(xlDown))
    
    For i = 9 To lastCell
        Worksheets("Sheet1").Cells(i, 2).Value = Application.WorksheetFunction.VLookup(firstRange, secondRange, 1, 0)
    Next
End Sub

Also if I tried to wrap my vlookup within an if(isna) but all the results just came out to Present, could this issue be related to the above or did setup the if condition incorrectly :
Excel Formula:
        Worksheets("Sheet1").Cells(i, 2).Value = _
        "=IF(ISNA(VLOOKUP(firstRange, secondRange, 1, False)),""Missing"", ""Present"")"
 

Attachments

  • vlookuptest.PNG
    vlookuptest.PNG
    12.1 KB · Views: 19

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
firstRange must be one cell not a range. Try: firstRange(i-8)
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

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