Vlookups in VBA using a current region

billyheanue

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

I am looking for a general theory as to go about using Vlookups with "CurrentRegion" arrays. This is because the lookup table is never strictly defined. That is,

1) The actual lookup table will be the current region of .. say "A37".

2) Have the col_index_number value refer to a literal value IN A SEPARATE CELL (e.g "K99")that is outside the table, and possibly even on a different sheet.

3) And finally, have the lookup value paste in the newly created/formatted column as given by:

Code:
Private Sub CommandButton3_Click()Set Rng = Range("A37").CurrentRegion
Rng.Select
Selection.Copy
numRows = Selection.Rows.Count
numColumns = Selection.Columns.Count
Selection.resize(numRows + 0, numColumns + 1).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False


End Sub

I think number 2 is giving me the most trouble. Would I just use

Code:
dim colValue = cells("K99")

...and then use the variable colValue instead of putting in an actual number when doing the formula in VBA?


Thanks Everyone
 
Hi Stephen,

When you determining the number of rows dynamically, what do you mean? Does that latter part require storing "lrows" as a variable? I get an error when trying to run it.

Thanks!
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Yes, lRows is a VBA variable.

Let's say you have lookup_values in a single column starting in Sheet1!B16.

Then depending on the way Sheet1 is organised, alternatives for determining lRows include:

Code:
Dim lRows As Long
Const START_ROW = 16

'If you know column B is empty below the lookup_values
lRows = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row - START_ROW + 1

'OR
'If you know there is at least one blank row between the lookup_values and further info in column B
With Sheets("Sheet1").Range("B" & START_ROW)
    If .Offset(1).Value = "" Then
        lRows = 1
    Else
        lRows = .End(xlDown).Row - START_ROW + 1
    End If
End With

'OR
'If your lookup_values are in a named range
lRows = Range("YourRangeName").Rows.Count
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,453
Members
449,161
Latest member
NHOJ

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