Why am I getting a type mismatch on certain items on a listbox but not others?

DrCheese1023

New Member
Joined
Jun 28, 2021
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Hey all! First off, here is my code:

VBA Code:
Private Sub PopoutLeadListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim selectedItem As String
selectedItem = Me.PopoutLeadListBox.Column(11)

SalesForm.BHSDTAPNAMELF.Value = Application.XLookup(Val(selectedItem), Worksheets("MASTER TAPS").Range("S:S"), Worksheets("MASTER TAPS").Range("T:T"))
SalesForm.BHSDCOMPANYNAMELF.Value = Application.XLookup(Val(selectedItem), Worksheets("MASTER TAPS").Range("S:S"), Worksheets("MASTER TAPS").Range("U:U"))
End Sub

This is working great on a few of the items in my list box. However, most of them give me a type mismatch error, and the fact that it's not working on some items but is on others with no discernible difference between the items is confusing.

Am I missing something simple?
 

Attachments

  • gsdfg23-06-08 153318.png
    gsdfg23-06-08 153318.png
    17.4 KB · Views: 9

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Let's check if they are numbers.

Try and tell us what the message says.

VBA Code:
Private Sub PopoutLeadListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim selectedItem As String
  selectedItem = Me.PopoutLeadListBox.Column(11)
 
  If IsNumeric(selectedItem) Then
    SalesForm.BHSDTAPNAMELF.Value = Application.Lookup(Val(selectedItem), Worksheets("MASTER TAPS").Range("S:S"), Worksheets("MASTER TAPS").Range("T:T"))
    SalesForm.BHSDCOMPANYNAMELF.Value = Application.Lookup(Val(selectedItem), Worksheets("MASTER TAPS").Range("S:S"), Worksheets("MASTER TAPS").Range("U:U"))
  Else
    MsgBox "It is not numeric: " & selectedItem
  End If
End Sub

:)

Or try this option:

VBA Code:
Private Sub PopoutLeadListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim selectedItem As String
  Dim f As Range
 
  selectedItem = Me.PopoutLeadListBox.Column(11)
  With Worksheets("MASTER TAPS")
    Set f = .Range("S:S").Find(selectedItem, , xlValues, xlWhole)
    If Not f Is Nothing Then
      SalesForm.BHSDTAPNAMELF.Value = .Range("T" & f.Row).Value
      SalesForm.BHSDCOMPANYNAMELF.Value = .Range("U" & f.Row).Value
    Else
      MsgBox "Not found: " & selectedItem
    End If
  End With
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------

 
Last edited:
Upvote 1
Solution
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Why am I getting a Type Mismatch error when using some items from a listbox in a function?
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Let's check if they are numbers.

Try and tell us what the message says.

VBA Code:
Private Sub PopoutLeadListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim selectedItem As String
  selectedItem = Me.PopoutLeadListBox.Column(11)
 
  If IsNumeric(selectedItem) Then
    SalesForm.BHSDTAPNAMELF.Value = Application.Lookup(Val(selectedItem), Worksheets("MASTER TAPS").Range("S:S"), Worksheets("MASTER TAPS").Range("T:T"))
    SalesForm.BHSDCOMPANYNAMELF.Value = Application.Lookup(Val(selectedItem), Worksheets("MASTER TAPS").Range("S:S"), Worksheets("MASTER TAPS").Range("U:U"))
  Else
    MsgBox "It is not numeric: " & selectedItem
  End If
End Sub

:)

Or try this option:

VBA Code:
Private Sub PopoutLeadListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim selectedItem As String
  Dim f As Range
 
  selectedItem = Me.PopoutLeadListBox.Column(11)
  With Worksheets("MASTER TAPS")
    Set f = .Range("S:S").Find(selectedItem, , xlValues, xlWhole)
    If Not f Is Nothing Then
      SalesForm.BHSDTAPNAMELF.Value = .Range("T" & f.Row).Value
      SalesForm.BHSDCOMPANYNAMELF.Value = .Range("U" & f.Row).Value
    Else
      MsgBox "Not found: " & selectedItem
    End If
  End With
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------

My issue was actually super simple. Some of the data was missing from the "MASTER TAPS" sheet and was pulling a type mismatch error because of that. The code was actually perfect, the data wasn't. Thank you so much, you helped me a lot. I was pulling my hair out.
 
Upvote 0

Forum statistics

Threads
1,215,309
Messages
6,124,180
Members
449,146
Latest member
el_gazar

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