Automatically change the col_index_num in vlookup?

ExcelNoob222

Board Regular
Joined
Jun 17, 2020
Messages
77
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

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
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
 
Upvote 0
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:
Upvote 0
I'm assuming your code has some means of figuring out which column to use?
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,664
Members
448,976
Latest member
sweeberry

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