Referencing range name in calculation

G

Guest

I have the following three lines of code:

Dim a As Single
a = Worksheets(Sheet1Name).Cells(Sheet1RowPointer, h).Value
a = Round(a * (1 + WorksheetFunction.VLookup(Cells(Sheet1RowPointer, _
Sheet1RespCodeColPointer), "OverheadLookup", 2)), 0)

I'm trying to perform a lookup using the range name "OverheadLookup" but I
get an error message when line 3 executes.

Any help would be greatly appreciated. Thanks.
 
G

Guest

Hi Bob,

I think the problem is with closing brackets-
It should be like this

a = Round(a * (1 + WorksheetFunction.VLookup(Cells(Sheet1RowPointer, _
Sheet1RespCodeColPointer), "OverheadLookup", 2, 0)))

Hope this helps!!
 
G

Guest

Bob,

Try it like this:

a = Round(a * (1 + WorksheetFunction.VLookup(Cells(Sheet1RowPointer, _
Sheet1RespCodeColPointer), Range("OverheadLookup"), 2)), 0)
 
G

Guest

Bob,

Try changing "OverheadLookup" for Range("OverheadLookup")

If the formula is correct, it should work.
 
G

Guest

Hi Bob,
There are 2 problems one with the closing brackets and one with the 'cells'.
Your formula should be like this-

a = Round(a * (1 + WorksheetFunction.VLookup(Cells(Sheet1RowPointer, _
Sheet1RespCodeColPointer).Value, "OverheadLookup", 2, 0)))

Hope this helps!!
 
G

Guest

Thanks to everyone for your helpful suggestions. In addition to omitting
"Range", I realized that "Cells(Sheet1RowPointer, Sheet1RespCodeColPointer)"
references a string (rather than a value). So the solution is as follows:

Dim RespCode As String
Dim a As Single

RespCode = Worksheets(Sheet1Name).Cells(Sheet1RowPointer, _
Sheet1RespCodeColPointer)

a = Worksheets(Sheet1Name).Cells(Sheet1RowPointer, h).Value

a = Round(a * (1 + WorksheetFunction.VLookup(RespCode, _
Range("OverheadLookup"), 2)), 0)

Thanks again,
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top