How to create a VLOOKUP in VBA with a variable array/column index dependent on number of columns

jdk8008

New Member
Joined
May 2, 2015
Messages
11
Office Version
  1. 2019
Platform
  1. MacOS
Hi there,

I was wondering if anyone had any ideas on how to accomplish this.

Currently, I have customer reports that I am creating macros for that analyzes the data on the report. The problem is the format of the report changes depending on which columns the customer wants to display:

Report Example 1
ABCDE
NameINV#IntTaxINV Amt
Smith, Brad1001$3.44$7.75$100.00

<tbody>
</tbody>






Report Example 2
ABC
NameINV#INV Amt
Smith, Brad1001$100.00

<tbody>
</tbody>

Report Example 3
ABCDEFG
NameINV#CredExpIntTaxINV Amt
Smith, Brad1001$25.00$15.00$3.44$7.75$100.00

<tbody>
</tbody>

The common denominator in all of the reports is that the column headers will always be named the same, but the column may or may not exist on the report. I was thinking maybe we could use header names as a reference point possibly? I don't know how to code this, but this it what would be in general logic:

lets say I want to display sheet1 INV#:
Report 1: IF header = Name, INV#, Int, Tax, INV Amt, then VLOOKUP(B2,'Sheet1'!B:E,1,0)
Report 2: IF header = Name, INV#, INV Amt, then VLOOKUP(B2,'Sheet1'!B:C,1,0)
Report 3: IF header = Name, INV#, Cred, Exp, Int, Tax, INV Amt, then VLOOKUP(B2,'Sheet1'!B:G,1,0)

If column header "X" exists, +1 column in the array of the VLOOKUP formula, if it doesn't -1 column in the array of the formula.

also

The column index could be variable. Lets say I want to display sheet1 tax:

Report 1: IF header = Name, INV#, Int, Tax, INV Amt, then VLOOKUP(B2,'Sheet1'!B:E,3,0)
Report 3: IF header = Name, INV#, Cred, Exp, Int, Tax, INV, then VLOOKUP(B2,'Sheet1'!B:G,4,0)



Here is basis of what I have now for the formula:

Range("D2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],Sheet1!C[-2]:C[-1],2,0)"

But I am not sure how to make this formula change based on which columns are included on the report. Any help would be great!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How do you want to specify the parameters to locate? Must the macro collect the lookup value from the sheet or are you passing it into the macro via an inputbox?
I would recommend using an INDEX(array,MATCH(...),MATCH(..)) setup that allows for a two directional lookup, vertically for the INV# and horizontally for the appropriate column header.
 
Upvote 0
How do you want to specify the parameters to locate? Must the macro collect the lookup value from the sheet or are you passing it into the macro via an inputbox?
I would recommend using an INDEX(array,MATCH(...),MATCH(..)) setup that allows for a two directional lookup, vertically for the INV# and horizontally for the appropriate column header.

That sounds much easier than what I was envisioning. I will give it a try. Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,018
Messages
6,122,703
Members
449,093
Latest member
Mnur

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