Resolve Type Mismatch error

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I'm getting a Type Mismatch error on Price = lbP11.Caption that I can not seem to resolve. lbP11 is a label that resides on a Userform. The caption is 144.17%. If the caption is $2,768.86, no error occurs. What am I am i missing or doing wrong.

thanks for your guidance.

VBA Code:
Private Sub lbN11_Click()
Dim Symbol As String
Dim Price As Double

If lbN11 = "" Then Exit Sub
Symbol = lbN11.Caption
Price = lbP11.Caption

FindBestWorst Symbol, Price
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try:

VBA Code:
Private Sub lbN11_Click()
  Dim Symbol As String
  Dim Price As Double
  
  If lbN11 = "" Then Exit Sub
  Symbol = lbN11.Caption
  
  If lbP11.Caption <> "" Then
    Price = CDbl(Replace(lbP11.Caption, "%", ""))
  End If
  
  FindBestWorst Symbol, Price
End Sub
 
Upvote 0
I've tried the "Replace" function already. While that error was eliminated, the code that I use in conjunction below looks for that cell value, which is 144.17%. If I remove the "%" then it will not find the value. The image below is the cell value.

1691714529176.png


VBA Code:
Private Sub FindBestWorst(ByVal Symbol As String, ByVal Price As Double)
Dim dataRange As Range, cell As Range
Dim LRow As Long
Dim columnIndex As Long

LRow = Application.Max(7, Range("A" & Rows.Count).End(xlUp).Row)
Set dataRange = Range("A7:S" & LRow)

columnIndex = IIf(obGnL, 18, 17)

For Each cell In dataRange.Rows
    If cell.Cells(1, 1).Value = Symbol And cell.Cells(1, columnIndex).Value = Price Then
        cell.Cells(1, 1).Select
        Exit For
    End If
Next cell
End Sub

Private Sub lbN11_Click()
Dim Symbol As String
Dim Price As Double

If lbN11 = "" Then Exit Sub
Symbol = lbN11.Caption
Price = lbP11.Caption

FindBestWorst Symbol, Price
End Sub
 
Upvote 0
First, you should consider the following, when you store values in combobox, textbox, listbox controls even labels, those values are text.
So, considering the above, if you want to use that value as a number, you have to convert that text to a number.

Second, in this particular case, to convert the text 144.17% to a number, you must remove the % but also divide by 100, since in the cell you will have 1.4417 (144.17%).

Third, hereafter put the complete code of the entire userform so that we can give you a complete solution from the beginning.

Fourth, this variable I don't know where it gets the value from.
Rich (BB code):
columnIndex = IIf(obGnL, 18, 17)

Then try the following.
Rich (BB code):
Private Sub lbN11_Click()
  Dim Symbol As String
  Dim Price As Double
  
  If lbN11 = "" Then Exit Sub
  Symbol = lbN11.Caption
  
  If lbP11.Caption <> "" Then
    Price = CDbl(Replace(lbP11.Caption, "%", "")) / 100
  End If
  
  Call FindBestWorst(Symbol, Price)
End Sub

🫡
 
Upvote 1
Solution
Hi Dante...Thank you for the explanation. I learn something new and valuable from your explanation.
This if the first time I've encounter this situation. The line of code worked.
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,803
Members
449,127
Latest member
Cyko

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