Lookup with Excel VBA

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
214
Office Version
  1. 365
Platform
  1. Windows
I have two sheets on the same WB.
WS, Original contains CUS_ID and MARKET_DES. WS, Test contains few fields like product, and MARKET_DES

I need product from Test to look up the values of CUS_ID of Original and fill the ST field in MARKET_DES with the values of MARKET_DESC from Original.

I am looking to do this using Excel VBA since I have other code for other functionalities.

I have pasted a sample of data.

I downloaded XL2BB but was unable to add it as ADD-IN on my work laptop

1658761872794.png


1658761936304.png
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I would recommend just using VBA to populate that column with a simple VLOOKUP or INDEX/MATCH formula.

It would look something like this:
VBA Code:
Sub MyMacro()

    Dim lr As Long
    
'   Find last row in column A on "Test" sheet with data
    lr = Sheets("Test").Cells(Rows.Count, "A").End(xlUp).Row

'   Populate column B with formula
    Sheets("Test").Range("B2:B" & lr).FormulaR1C1 = "=VLOOKUP(RC[-1],Original!C[-1]:C,2,0)"

End Sub
 
Upvote 0
I would recommend just using VBA to populate that column with a simple VLOOKUP or INDEX/MATCH formula.

It would look something like this:
VBA Code:
Sub MyMacro()

    Dim lr As Long
  
'   Find last row in column A on "Test" sheet with data
    lr = Sheets("Test").Cells(Rows.Count, "A").End(xlUp).Row

'   Populate column B with formula
    Sheets("Test").Range("B2:B" & lr).FormulaR1C1 = "=VLOOKUP(RC[-1],Original!C[-1]:C,2,0)"

End Sub
Hi,

Can you explain what C refers to? VLOOKUP(RC[-1],Original!C[-1]:C,2,0)"

Since I need values from column B in Original to be populated in col B of Test, C[-1] is col B of Original?

So if in Original, the values are in col D, then the formula will be "=VLOOKUP(RC[-1],Original!E[-1]:E,2,0)"?
 
Upvote 0
Can you explain what C refers to? VLOOKUP(RC[-1],Original!C[-1]:C,2,0)"

Since I need values from column B in Original to be populated in col B of Test, C[-1] is col B of Original?

So if in Original, the values are in col D, then the formula will be "=VLOOKUP(RC[-1],Original!E[-1]:E,2,0)"?
No, the "C" has nothing to do with Column letter C.
It is R1C1 reference (where "R" stands for "Row" and "C" stands for "Column").

See here: Excel Formulas - A1 or R1C1 Notation

Note that the good news is you do not even need to know how to write this, you can let Excel do it for !
All you need to do is turn on your Macro Recorder, and record yourself manually entering the formula in the first cell (where you want it to go).
Then, you stop the Macro Recorder and view the code you just recorded - it will give you the formula EXACTLY as you need it (since it is using relative referencing).
So then you just copy/paste that into your VBA code.

By the way, did you try running my code?
If you did, and the structure you posted in your original post is accurate, I think you will find it does exactly what you want.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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