Combobox Type Mismatch is driving me insane

TheDude0901

New Member
Joined
Jan 9, 2014
Messages
10
Greetings,

I'm trying to create a very simple Userform with a single combobox named "combobox1" to select a product part number and fill in three textboxes with related data. The problem that I'm having is when I select the first part number that is a string my code works perfectly. If I select any of the numeric part numbers I get a "Type Mismatch" error at the line indicated below.

It needs to work with both integers and strings as our part numbers are all over the board. Some may be "1234567" or "123-45-67" or even "AXL491-48115"

Yes, I'm a VBA newbie and I'm completely at my wits end. Any help would be very greatly appreciated!

Best regards,
The Dude


Code:
Private Sub UserForm_Initialize()
    Populate_Combobox1
    ComboBox1.SetFocus
End Sub

Private Sub ComboBox1_Click()
     Show_Paint_Data (ComboBox1.Value)
End Sub

Private Sub Populate_Combobox1()
    Dim v, e
    With Sheets("Sheet1").Range("A2:A20")
        v = .Value
    End With
    With CreateObject("scripting.dictionary")
        .comparemode = 1
        For Each e In v
            If Not .exists(e) And e <> "" Then .Add e, Nothing
        Next
        If .Count Then Me.ComboBox1.List = Application.Transpose(.keys)
    End With
End Sub

Private Sub Show_Paint_Data(part_number As String)
    Dim x As Integer
    Dim row_number As Integer
    Dim rng1 As Range
    Dim rng2 As Range
    
    Set rng1 = Application.Worksheets("Sheet1").Range("A2:A20")
    Set rng2 = Application.Worksheets("Sheet1").Range("B2:R20")
    
    row_number = Application.Match(part_number, rng1, 0)                 [SIZE=4][I][B][COLOR=#ff0000] <-------- BLOWS UP HERE[/COLOR][/B][/I][/SIZE]
    x = 1
    
    Do While x < 4
        Controls("Ident_" & x).Value = Application.WorksheetFunction.Index(rng2, row_number, x)
        x = x + 1
    Loop
    
End Sub



Part NumberField 1Field 2Field 3
ZSDF234
2333
3123

<tbody>
</tbody>
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try dimensioning row_number as Variant. And then testing IsError(row_number) to see if part_number has been found by the Match.
 
Upvote 0
Try dimensioning row_number as Variant. And then testing IsError(row_number) to see if part_number has been found by the Match.

I changed it to the following:

Code:
Private Sub Show_Paint_Data(part_number As String)
    Dim x As Integer
    Dim row_number As Variant
    Dim rng1 As Range
    Dim rng2 As Range
    
    Set rng1 = Application.Worksheets("Sheet1").Range("A2:A20")
    Set rng2 = Application.Worksheets("Sheet1").Range("B2:R20")
    row_number = Application.Match(part_number, rng1, 0)
    x = 1
    
    If IsError(row_number) Then
        MsgBox "error"
    Else
        MsgBox row_number
    End If
    
    Do While x < 4
        Controls("Ident_" & x).Value = Application.WorksheetFunction.Index(rng2, row_number, x)
        x = x + 1
    Loop
    
End Sub

When I selected the string part number the message box showed the correct row number. Selecting an integer part number the message box showed "error".
 
Upvote 0
I tried this as well.

It will pop up a message box with "error" but I assume that's because of the type mismatch error and not being able to match it to the number.


Code:
Private Sub Show_Paint_Data(part_number As String)
    Dim x As Integer
    Dim row_number As Variant
    Dim rng1 As Range
    Dim rng2 As Range
    
    Set rng1 = Application.Worksheets("Sheet1").Range("A2:A20")
    Set rng2 = Application.Worksheets("Sheet1").Range("B2:R20")
    
    'row_number = Application.Match(part_number, rng1, 0)
    x = 1
    
    If IsError(Application.Match(part_number, rng1, 0)) Then
        MsgBox "error"
    Else
        MsgBox row_number
    End If
    
    Do While x < 4
        Controls("Ident_" & x).Value = Application.WorksheetFunction.Index(rng2, Application.Match(part_number, rng1, 0), x)
        x = x + 1
    Loop
    
End Sub
 
Upvote 0
May be you should change "Value" property to "List"?
 
Upvote 0
You could also change the type of part_number to variant

Code:
Private Sub Show_Paint_Data(part_number As Variant)
    ' ...

    If IsNumeric(part_number) then part_number = Val(part_number)
   
    Set rng1 = Application.Worksheets("Sheet1").Range("A2:A20")
' etc
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,183
Members
449,071
Latest member
cdnMech

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