Automatically change the col_index_num in vlookup?

ExcelNoob222

Board Regular
Joined
Jun 17, 2020
Messages
76
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a vlookup that currently returns the value in the last column, which happens to be column E currently. So my vlookup is: =VLOOKUP(A2,Data!A:E,5,FALSE)

I have a macro that inserts this formula. However, at some point data will be added to column F and then eventually G and so on. How can I adjust the vba so that the col_index_num automatically changes to the last column of data? So that when data gets added to column F the formula would be =VLOOKUP(A2,Data!A:E,6,FALSE) and then when added to column G it is =VLOOKUP(A2,Data!A:E,7,FALSE)?

Thanks!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
39,349
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
That won't work since you also need to amend the A:E part.

I'd suggest you use INDEX and MATCH instead so you only have one range to adjust (the red part):

=INDEX(Data!E:E,MATCH(A2,Data!A:A,0))

If you're doing it in code, use the R1C1 reference style and it's even simpler, assuming you have a variable with the relevant column number in it:

Code:
Range("B2").formular1c1 = "=INDEX(Data!C" & colNumber & ",MATCH(RC1,Data!C1,0))"

for example
 

ExcelNoob222

Board Regular
Joined
Jun 17, 2020
Messages
76
Office Version
  1. 365
Platform
  1. Windows
That won't work since you also need to amend the A:E part.

I'd suggest you use INDEX and MATCH instead so you only have one range to adjust (the red part):

=INDEX(Data!E:E,MATCH(A2,Data!A:A,0))

If you're doing it in code, use the R1C1 reference style and it's even simpler, assuming you have a variable with the relevant column number in it:

Code:
Range("B2").formular1c1 = "=INDEX(Data!C" & colNumber & ",MATCH(RC1,Data!C1,0))"

for example

I should have mentioned I would adjust the A:E part in advance. I would do like A:ZZ

Won't the index match not work either because the Data!E:E column would change as new data is added?

I think I just thought of a way.

In a cell I can do =COUNTA(A2:ZZ2) which would count my headers. Then my vlookup formula would be: =VLOOKUP(A3,Data!A:K,Data!A1,FALSE). As new headers are added with the new data, the count will auto update.
 
Last edited:

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
39,349
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
I'm assuming your code has some means of figuring out which column to use?
 

Forum statistics

Threads
1,176,266
Messages
5,902,244
Members
434,954
Latest member
kchallr00

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