newExcelSheet
New Member
- Joined
- Apr 4, 2021
- Messages
- 2
- Office Version
- 365
- Platform
- 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.
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 :
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"")"