VBA Vlookup returning Type Mismatch

scoha

Active Member
Joined
Jun 15, 2005
Messages
428
I am having trouble getting my VBA Vlookup to work and hope the exterts out there might be able to assist.

I use a global variable to transfer a variable value from another userform within the same workbook.

Code:
Private Sub UserForm_Initialize()

Dim ws As Worksheet
Set ws = Worksheets("Contingency")

txtTCID = GlobalContractID

End Sub

The bidea is then to place this number into a text box (called txtTContractID) and use the change to then go and lookup the values for the other text boxes, along the lines of:

Code:
Private Sub txtTCID_Change()

txtTProjectID.Value = Application.VLookup(Me.txtTCID, Range("tblContracts"), 2, False)

End Sub


but I get a "Could not set the Value property - Type Mismatch error' The lookup up is not returning the right value.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Don't know if it is due to different Excel version but the syntax is usually
Application.WorksheetFunction.VLookup()

VLOOKUP() is often a problem. The find value and lookup range must be formatted the same to get a match. In this case, using a textbox, the find value will be a string(text).

Try formatting the lookup range as text. That will probably not be enough. We also need to force the conversion by selecting the column and using Data/Text to columns...Finish.

*Sometimes* in cases like this Excel does not like doing too much work in a single line. Could try
.......... Vlookup(GlobalContractID,............
Because it is already a variable, not looking up a textbox value. Need to Dim the variable type of GlobalContractID As String or numeric type.
 
Upvote 0
If you are looking up a number why are you using a textbox?

The least you could do is wrap it with the Val function.:)
Code:
Private Sub txtTCID_Change()

txtTProjectID.Value = Application.VLookup(Val(Me.txtTCID), Range("tblContracts"), 2, False)

End Sub
PS What global variable are you talking about?

I don't see any variables, never mind global ones, declared in the code you've posted.:)
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

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