Vlookup in VBA generating error

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to condense following:
Code:
    Select Case i
        Case 1 To 3:    ForeCastCol = "A"
        Case 4 To 10:   ForeCastCol = "B"
        Case 11 To 15:  ForeCastCol = "C"
        Case Else:      ForeCastCol = "D" 'Limit is 18 regardless
    End Select
To:
Code:
Application.Vlookup(i, [3, 10, 15, 18], ["A", "B", "C", "D"], True)
Where "A", "B" etc are irrelevant or an equivalent function

Limits of 3, 10, 15 and 18 will never change (and always ascending order) can someone suggest?

TIA,
Jack
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about
Code:
Dim x As Variant, i As Long
i = 5
x = Evaluate("Lookup(" & i & ",{0,4,11,16},{""A"",""B"",""C"",""D""})")
 
Upvote 0
Or:
Code:
i = 5
x=Application.Lookup(i, Array(3, 10, 15, 18), Array("A", "B", "C", "D"))
 
Upvote 0
Array(), that's what I wasn't using for 2nd and 3rd arguments, thanks too @JoeMo!
 
Upvote 0
Hi @Fluff and @JoeMo, may have spoken too soon. This is returning Error 2042, I replaced "5" with i (after realising I was getting the same results for each loop!)
Rich (BB code):
Private Function ForeCastCol(ByRef i As Long) As Variant
        
    ForeCastCol = Application.Lookup(i, Array(3, 10, 15, 18), Array("Total_", "Practices_", "Central_", "AffL_"))
    
    '& wMain.Cells(wMain.Range("Head_Job").Row, wMain.Range("Head_Cal").Column).Resize(, 18).Cells(1, i).Value
    
End Function
Can you spot my error please? I've removed the concatenate of a specific header value (based on i from a fixed header area) to try to isolate the cause but can't work it out.
 
Upvote 0
It is @shg? If I revert to that, then won't have these errors...

Thanks for the comment @shg, curious to know why this doesn't work now, but if Select Case is faster (and working) then, will revert.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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