How to loop a Vlookup macro for each value in a column

billyheanue

Board Regular
Joined
Jul 13, 2015
Messages
109
Hi Everyone,

So I have code that successfully vlookups ONE specific cell, in a column of "Lookup Values".

Code:
Private Sub CommandButton4_Click()


LookupVal = Sheets("Sheet1").Range("B16").Value
Range("A5").Formula = "=VLOOKUP(" & LookupVal & ",Sheet2!" & Sheets("Sheet2").Range("A4").CurrentRegion.Address & _
",Sheet1!G13,FALSE)"


End Sub


Im wondering how I would go about having the code also continue on the column, and lookup the next cell "B17", and then "B18", and so forth, until the values in the column become blank. I also want to emphasize that the code needs to keep the current region bit!

Is this a "For Each...Next" loop?

Thanks Everyone!!
 

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
Hi,

I am not sure that I know what you are doing exactly: A5, B16 and G13 sounds a bit confusing to me :confused:

Also, the CurrentRegion part can be done differently. Why don't you make the CurrentRegion on that worksheet a Table? You just need to go to the Insert tab and select Insert-->Table. It will then assign a name to the area. Assuming it is your first table it will be called Table1 - but you can rename it if you want.

Then your formula would look like this:

Code:
Private Sub CommandButton4_Click()

    Dim lr As Long
    
    With ThisWorkbook.Worksheets("Sheet1")
        lr = .Cells(.Rows.Count, "B").End(xlUp).Row
        .Range("A5:A" & lr).Formula = "=VLOOKUP(B16,Table1,$G$13,FALSE)"
    End With

End Sub

I have made a few assumptions there:

1. Cell A5 is on Sheet1.
2. The end of Column B on Sheet1 will tell you how many rows need filling.
3. The look-up data has been changed into a table called Table1.
4. $G$13 does not need to change as it is copied down because it contains a constant column number.
 
Upvote 0
Thanks Rick for your response. I'll try to make what I am trying to do clearer. On Sheet 1, I have a list of lookup values - approximately 8 numbers I need to do vlookups on. The lookup table is on sheet two. It needs to be a current region reference (current region of A4, sheet 2), because I need to perform this macro many times, and the lookup table dimensions are not constant.

I am trying to lookup the values on sheet one (beginning with the value in cell B16), and then print the vlookup results on sheet one as well, with the first result being printed in cell A5. So...

Cell B17 (sheet1) would print its vlookup result on A6
B18 would print its result on A7...

and so forth until the values in the list are all covered.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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