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

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,091
Office Version
  1. 2016
Platform
  1. Windows
firstRange must be one cell not a range. Try: firstRange(i-8)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,486
Messages
5,636,610
Members
416,929
Latest member
Nitil

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
Top