Error when exiting textbox that is blank.

Darth_Sullivan

New Member
Joined
Oct 23, 2013
Messages
48
The code below is supposed to look up the value of the number in a textbox named PCNumber and display a returned value in a label named PlayerName. This code works just as it is supposed to, except when the textbox is left blank.

Code:
Private Sub PCNumber_Exit(ByVal Cancel As MSForms.ReturnBoolean)

'Active search for the PCNumber and return the PlayerName to the Label
Dim Ans As Variant
Dim myVal As Long
Dim myRng As Range

Set myRng = Worksheets("Sheet1").Range("A1:B65536")

myVal = PCNumber

Ans = Application.VLookup(myVal, myRng, 1, False)

If IsError(Ans) Then
   Ans = "Not found!"
Else
   Ans = Application.VLookup(myVal, myRng, 2, False)
End If

PlayerName = Ans


End Sub

When the textbox is blank and the user clicks out of the textbox, I get:

Run-time error '13':
Type mismatch

When I then hit 'Debug' the piece of code highlighted is:

myVal = PCNumber

I've tried several different ways of trying to avoid this error using an IF block, but without success.

I should also add, at one point, if the textbox never had a entry to begin with, I did not get an error. I would only get the error, during that stage of form completion, when the user would start to type but then backspaced to clear their entry. That problem was solved by placing the code into the 'textbox_exit' routine instead of _change.

How do I keep from getting this error when the textbox is left blank?

Thank you for your time.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Solution found.

I decided to attach the code to a button and made the button only visible if the textbox contained anything other than blank. Made the button set to hidden when the form was entered and visible when textbox contained anything other than blank and back to hidden if the textbox became blank again.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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