Type Mismatch using CLng(with a text value of numbers)

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,566
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Rich (BB code):
Private Sub empl_no2_afterupdate()
    If Not mbevents Then Exit Sub

    'employee number error checking
    If Not empl_no2.Value Like "#####" Then
        MsgBox "Employee number has to be 5 numbers. a", , "ERROR"
        With empl_no2
            .Text = "00000"
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
        Exit Sub
    ElseIf Len(empl_no2.Value) <> 5 Then
        MsgBox "Employee number has to be 5 numbers. b", , "ERROR"
        With empl_no2
            .Text = "00000"
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
        Exit Sub
    ElseIf CLng(empl_no2.Value) < 11110 Or CLng(empl_no2.Value) > 99999 Then
        MsgBox "Employee number has to be 5 numbers. c", , "ERROR"
        With empl_no2
            .Text = "00000"
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
        Exit Sub
    End If

    'duplicate employee number
    If Application.WorksheetFunction.CountIf(CLng(empl_no2.Value), ws_rstr.Columns(1)) > 0 Then
        MsgBox "This employee number is already assigned to " & Application.WorksheetFunction.VLookup(ty, ws_rster.Range("A:E"), 5, False)
    End If

End Sub

I am receiving a "Type mismatch" with the line in red as it refers to the CLng(empl_no2.value) portion.

empl_no2 is a 5 character (only numbers) text value in a userform textbox (empl_no2). I am trying to use it's value in a match function but am getting an error, perhaps because I'm trying to compare text to the numeric data in column 1 of worksheet ws_rstr. That is why I put the CLng preceeding it. I thought that would work because I had to do the same thing in the line in blue in order to compare the text with the numbers. I don't get that error on that line.

I don't know what the difference is that it would react with an error in one case, but not the other. What is the solution to resolving this problem?
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You have the order of arguments reversed.
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,996
Members
452,542
Latest member
Bricklin

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