help with vlookup table_array

Honey

New Member
Joined
Sep 22, 2006
Messages
4
Hi,

I'm trying to build a macro that uses a vlookup without using a set table array...since the numbers of rows i'm using to lookup would change monthly. Is it possible for me to set a variable to the table array and then call the variable inside the vlookup formula?

In my macro I'm using the following:
=VLOOKUP(A91,'Current Crosstab'!A1:P200,16,FALSE)

However, next month...this current crosstab worksheet may have up to 300 rows. I don't want to hardcode a large number, I want to be able to count the rows in Current Crosstab and then have the P column update itself in the vlookup formula. Any ideas on how to best accomplish this?

Thanks in advance for your help.
Honey
 

Some videos you may like

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

richardrb

New Member
Joined
Sep 28, 2006
Messages
2
I'm no expert but can you name the range for the data output and then refer to the named range in the vlookup. As you add rows in they are included in the named range however not sure if it will work with "an update", If you have a feel for likely number of rows output you could simply name a larger range?
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,871
Hi Honey
Welcome to the board

If you are doing it in vba why the vlookup? In this case you don't need worksheet functions. Consider using the Find method.

This example considers the table with a variable number of rows. Looks for A91 of the active sheet in the Worksheet "Current Crosstab" in the range from A1 to the last cell in A with data, and returns the value in the row of the found value in column P.

Code:
Dim rFound As Range, vRes

With Worksheets("Current Crosstab")
    Set rFound = .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row) _
          .Find(what:=Range("A91").Value, LookIn:=xlValues, lookat:=xlWhole)
    If rFound Is Nothing Then
        vRes = "Not found"
    Else
        vRes = .Cells(rFound.Row, "P").Value
    End If
End With

Hope this helps
PGC
 

Watch MrExcel Video

Forum statistics

Threads
1,114,676
Messages
5,549,373
Members
410,911
Latest member
AniEx
Top