Vlookup with Match

HGDantes

New Member
Joined
Feb 10, 2023
Messages
16
Office Version
  1. 2019
Platform
  1. Windows
Hello

I'm trying to modify these line of Script :

VBA Code:
    Sheets("OnPrem").Select
    Range ("A2").Select
    ActiveCell.Value = "=IFERROR(VLOOKUP(" & "G2" & ",Previous_OnPrem!A:J,2,0),"""")"
    ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":" & "A" & RowCount)

But I would Like to Match with a column name "Previous_OnPrem" Sheet.

I've the following, but it's not working tried :

VBA Code:
ActiveCell.Value = "=IFERROR(VLOOKUP(" & "G2" & ",Previous_OnPrem!A:J,MATCH("Date",Previous_OnPrem!A:J,0)),"""")"
ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":" & "A" & RowCount)

"Date" Being the column name on the "Previous_OnPrem" sheet.

Also, I'm not sure if the name on column Must be the exact same on both sheets, But I have several column to match in several sheets, and Column names aren't always the same.

Thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Firstly, try
VBA Code:
ActiveCell.Value = "=IFERROR(VLOOKUP(" & "G2" & ",Previous_OnPrem!A:J,MATCH("Date",Previous_OnPrem!A:J,0),0),"""")"
as there is no argument for the match type in the VLOOKUP

If you are looking for Text, it will always look for an exact match. Are you looking for the word "Date" on the other sheets?
 
Last edited:
Upvote 0
You are using Match to work out the Column position, as such the Match Formula needs to be looking in a "single" row being the row with the headings in it.
The below assumes your row headings are in row 1, modify it if that is not the case.
Also your fill down is based on the ActiveCell but filling down column A. That is only going to work if the Activecell is in column A

Rich (BB code):
ActiveCell.Value = "=IFERROR(VLOOKUP(" & Range("G2").Address(0, 0) & ",Previous_OnPrem!A:J, MATCH(""Date"",Previous_OnPrem!A$1:J$1,0)),"""")"
ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":" & "A" & RowCount)
 
Upvote 0
Firstly, try
VBA Code:
ActiveCell.Value = "=IFERROR(VLOOKUP(" & "G2" & ",Previous_OnPrem!A:J,MATCH("Date",Previous_OnPrem!A:J,0),0),"""")"
as there is no argument for the match type in the VLOOKUP

If you are looking for Text, it will always look for an exact match. Are you looking for the word "Date" on the other sheets?
I'm actually looking to find the Header called "Date" and get data from that column. I have about 70 Vlookups on my Script and each time I'm adding or removing a column, I must go over the entire script and modify all VLookup Indexes. So I'm trying to find a better solution to match data.
Basically, I have 3 Sheets to fill in my report, and Getting data From 7 other reports, all from different sources.
 
Upvote 0
You are using Match to work out the Column position, as such the Match Formula needs to be looking in a "single" row being the row with the headings in it.
The below assumes your row headings are in row 1, modify it if that is not the case.
Also your fill down is based on the ActiveCell but filling down column A. That is only going to work if the Activecell is in column A

Rich (BB code):
ActiveCell.Value = "=IFERROR(VLOOKUP(" & Range("G2").Address(0, 0) & ",Previous_OnPrem!A:J, MATCH(""Date"",Previous_OnPrem!A$1:J$1,0)),"""")"
ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":" & "A" & RowCount)

Thanks, I will try this
 
Upvote 0
Thanks, I will try this
It didn't worked as expexted.

I'm actually looking to find the Header called "Date" in the Sheet "Previous_OnPrem" and get data from that column. I have about 70 Vlookups on my Script and each time I'm adding or removing a column, I must go over the entire script and modify all VLookup Indexes. So I'm trying to find a better solution to match data.
Basically, I have 3 Sheets to fill in my report, and Getting data From 7 other reports, all from different sources.
One other issue is that names From Sources Files aren't always exactly the same as on my reports.
 
Upvote 0
Does the row number for the heading row vary ?.
If it does perhaps use find to get the column & row and use the address of the Find in your Vlookup
 
Upvote 0
Does the row number for the heading row vary ?.
If it does perhaps use find to get the column & row and use the address of the Find in your Vlookup

Yup that's the Problem, It may vary. That's why I was looking for a solution because everytime a column change, I have to review my entire script and re-index all my Vlookups.

But I think I found the Best solution, I just tried it and it seems to work..


VBA Code:
    Dim UpdateDateSource As Long
    Sheets(Previous_OnPrem).Select
    UpdateDateSource = WorksheetFunction.Match("Date", Rows("1:1"), 0)
    Sheets("OnPrem").Select
    Range ("A2").Select
    ActiveCell.Value = "=IFERROR(VLOOKUP(" & "G2" & ",Previous_OnPrem!A:J,UpdateDateSource,0),"""")"
    ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":" & "A" & RowCount)
 
Last edited:
Upvote 0
I asked if the Row number for heading varies, you responded with Yup but then talk about the Column changing.

If your latest post works for you then great but it is no different to the formula I gave you in that it hard codes the row as Row 1
The only difference is that since you are hardcoding A:J in you Vlookup anyway I have restricted the Row 1 search to A:J as well.
Something like this would hard code the column into the actual formula but would find the column to use, regardless of which row the heading is on.


Rich (BB code):
Dim prevSht As Worksheet
Dim foundCell As Range
Dim foundCol As Long
Dim findStr As String
Dim findRng As Range

Set prevSht = Worksheets("Previous_OnPrem")
findStr = "Date"
Set findRng = prevSht.Range("A:J")

    Set foundCell = findRng.Find(What:=findStr, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
       
    ActiveCell.Value = "=IFERROR(VLOOKUP(" & Range("G2").Address(0, 0) & ",Previous_OnPrem!A:J, " & foundCell.Column & "),"""")"
    ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":" & "A" & RowCount)
 
Upvote 0
Solution
I asked if the Row number for heading varies, you responded with Yup but then talk about the Column changing.

If your latest post works for you then great but it is no different to the formula I gave you in that it hard codes the row as Row 1
The only difference is that since you are hardcoding A:J in you Vlookup anyway I have restricted the Row 1 search to A:J as well.
Something like this would hard code the column into the actual formula but would find the column to use, regardless of which row the heading is on.


Rich (BB code):
Dim prevSht As Worksheet
Dim foundCell As Range
Dim foundCol As Long
Dim findStr As String
Dim findRng As Range

Set prevSht = Worksheets("Previous_OnPrem")
findStr = "Date"
Set findRng = prevSht.Range("A:J")

    Set foundCell = findRng.Find(What:=findStr, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
     
    ActiveCell.Value = "=IFERROR(VLOOKUP(" & Range("G2").Address(0, 0) & ",Previous_OnPrem!A:J, " & foundCell.Column & "),"""")"
    ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":" & "A" & RowCount)

Oh Sorry, I read too fast I guess. No The Row for Headers is always Row 1

Thanks. I'll try this solution as well. Not sure which one will be the fastest to run. With Over 45 000 lines and about 70 Vlookups, it may takes 15-20 minutes to run, so if I can save even a minute or 2 That would be nice.. haha

Thanks for you help
 
Upvote 0

Forum statistics

Threads
1,215,636
Messages
6,125,951
Members
449,276
Latest member
surendra75

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