views:

593

answers:

4

All,

I'm trying to use vlookup in a simple VBA function, but it is continually returning #VALUE!

Here is the code:

Public Function getAreaName(UBR As Integer) As String

  Dim result As String
  Dim sheet As Worksheet
  Set sheet = ActiveWorkbook.Sheets("UBR Report")
  ' check level 3 then 2 then 4 then 5
  result = Application.WorksheetFunction.VLookup(UBR, sheet.Range("UBRLookup"), Application.WorksheetFunction.Column(sheet.Range("UBRLookup[Level 3]")), False)
  getAreaName = result

End Function

Any thoughts?

+1  A: 

Break you function up into more pieces. Then debug it and make sure every piece is set up the way you expect.

Example:

Public Function getAreaName(UBR As Integer) As String
    Dim result As String
    Dim sheet As Worksheet
    Set sheet = ActiveWorkbook.Sheets("UBR Report")
    Dim range as Range = sheet.Range("UBRLookup")
    Dim column as Column = Application.WorksheetFunction
            .Column(sheet.Range("UBRLookup[Level 3]"))
    result = Application.WorksheetFunction.VLookup(UBR, range, column, False)
    getAreaName = result
End Function

In fact, just by doing that I noticed something weird. You use a range in two different places, but in one place you're looking for UBRLookup, and in another you're looking for UBRLookup[Level 3], is that correct?

Joseph
Cannot work. Column is not a valid data type. -1
iDevlop
@iDevlop it's air code. An Example to show him what I meant by break up the pieces. I don't know what Application.WorksheetFunction.Column() returns it was a guess, that's for the OP to figure out.
Joseph
+2  A: 

I'm not quite sure what you're trying to do with the "UBRLookup[Level 3]" reference, but as Joseph has pointed out, that's the bit that you're doing wrong.

[ is not a valid character for a named range in Excel.

The column that you're referencing needs to be a numeric value, the offset from the start of the table-array you've defined as your named range.

The below should work, provided the column you want to pull out is the second column in your named range (e.g. what you're referring to as [level 3] is in the second column).

Public Function getAreaName(UBR As Integer) As String

  Dim result As String
  Dim sheet As Worksheet
  Set sheet = ActiveWorkbook.Sheets("UBR Report")
  result = Application.WorksheetFunction.VLookup(UBR, sheet.Range("UBRLookup"), 2, False)
  getAreaName = result

End Function

Update:
I've had a look at Excel 2007 and from what I can see the column function isn't exposed as an Application.WorksheetFunction. You can use it on the sheet with =Column(D4), but when trying to autocomplete within the vba editor, the function isn't there. This may be due to a difference in versions, so I'll ignore that for now.

It still definitely seems like you're mis-using the third argument. If you really don't want to use the number reference we need to find out where the function is going wrong.

A few tests along the lines of

Debug.Print Application.WorksheetFunction.Column(D4)
Debug.Print sheet.Range("UBRLookup[Level 3]")

should hopefully help to show you exactly where it's going wrong - I believe that it will object to both of the above, but if it returns some useful information then we may be a step closer to your solution.

Andy
I'm using the excel 2007 tablename[column] syntax and the column() function to automagically determine the offset. However, I'll try it with the manual setting.
jwoolard
Updated the above. Do the Debug.Print comments run on your version of Excel 2007?
Andy
+1  A: 

I am disturbed by
Dim column as Column =
Application.WorksheetFunction.Column(sheet.Range("UBRLookup[Level 3]"))

You should Dim column as long, I think, and maybe use a variable name that's not to be confused with a property, like lngCol.

iDevlop
I'm surprised the original version even compiles. I get "User-defined type not defined" in Excel 2003 and I don't see a Column object in the Excel 2007 object model either
barrowc
Right.It should be: dim lngCol as longlngCol = sheetx.range("xxx").column
iDevlop
A: 

This part: sheet.Range("UBRLookup[Level 3]") is suspect as "UBRLookup[Level 3]" is not a valid range name.

Oorang