Vlookpup with VBA

vitalis1023

New Member
Joined
Mar 30, 2013
Messages
3
I am trying to write a function in VBA that uses Vlookup function for a table in excel. Here is what I have so far but I am getting an overflow error. Note: when I set myLookup = RandomValue (that is pulling a random value generated in another function named "RandomValue".

Public Function RandomDay() As Long
Dim myRng As Object
Dim myLookup As Integer
Set myRng = Worksheets("Sheet7").Range("B2:C366")
myLookup = RandomValue

RandomDay = WorksheetFunction.VLookup(myLookup, myRng, 2, True)

Debug.Print RandomDay
End Function
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I am trying to write a function in VBA that uses Vlookup function for a table in excel. Here is what I have so far but I am getting an overflow error. Note: when I set myLookup = RandomValue (that is pulling a random value generated in another function named "RandomValue".

Public Function RandomDay() As Long
Dim myRng As Object
Dim myLookup As Integer
Set myRng = Worksheets("Sheet7").Range("B2:C366")
myLookup = RandomValue

Debug.Print MyLookup

RandomDay = WorksheetFunction.VLookup(myLookup, myRng, 2, True)

Debug.Print RandomDay
End Function
Try adding the line of code I highlighted in red and make sure the myLookup variable contains a value in a range that you expect it to be in.
 
Upvote 0
When I added Debug.Print myLookup the code still did not run. I did however, check the function that produces the value RandomValue and it is producing values in the correct range.
 
Upvote 0
When I added Debug.Print myLookup the code still did not run. I did however, check the function that produces the value RandomValue and it is producing values in the correct range.
Then on what line of code are you getting the "overflow error" you reported in your first message? If it is this one...

myLookup = RandomValue

then try changing your declaration of myLookup from Integer to Long.
 
Upvote 0
Just figured it out: this works

Public Function RandomDay() As Long
Dim myRng As Object
Set myRng = Worksheets("Sheet7").Range("B2:C366")

RandomDay = WorksheetFunction.VLookup(RandomValue(), myRng, 2, True)

Debug.Print RandomDay

End Function
 
Upvote 0
Just figured it out: this works

Public Function RandomDay() As Long
Dim myRng As Object
Set myRng = Worksheets("Sheet7").Range("B2:C366")

RandomDay = WorksheetFunction.VLookup(RandomValue(), myRng, 2, True)

Debug.Print RandomDay

End Function
More than likely your original problem was what I indicated in Message #4... you had the wrong declaration for the variable (myLookup) given the size of the number the RandomValue function was returning to it. By removing the variable and assigning the function return value directly to the first argument of the VLookup function, you bypassed the problem you created by using the wrong data type for myLookup.
 
Upvote 0
:LOL:

I played with VLOOKUP for a while and eventually posted a suggestion, but I took so long to do it that the problem was solved by then.

So I deleted it.
 
Upvote 0

Forum statistics

Threads
1,203,094
Messages
6,053,503
Members
444,667
Latest member
KWR21

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