Runtime error '13'

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I have a workbook that auto filters results in another workbooks based the value of a range. The code works great when only numbers are being used but when a letter is introduced into the cell, I get a RUNTIME ERROR '13', TYPE MISMATCH debug error.

For example, the code works great if I use "512022" but if I use "512022-U" or "512022U" I get the runtime error. Code error is in red.

Code:
Private Sub CommandButton1_Click()
Dim rng As Range
Dim CAFval As Double

Application.ScreenUpdating = False

Sheets("MDF").Range("ProductCode").Value = TextBox1.Value
Sheets("Porosity").Range("f2").Value = TextBox1.Value
Sheets("Data Sheet").Range("ProductCode").Value = TextBox1.Value

'open CAF Spreadsheet and auto filter results based on product code

Application.ThisWorkbook.FollowHyperlink "P:\Lab Folder\CAF\CAF 10.xls"
 
Set rng = Workbooks("CAF 10.xls").Sheets("Main").Range("$B$5:$N$891")
[COLOR=#ff0000]Let CAFval = Workbooks("Quality Control Data Sheet (BETA).xls").Sheets("Data Sheet").Range[/COLOR]
("ProductCode").Value
       
            With rng
                .AutoFilter field:=1, Criteria1:=CAFval
        End With
Windows("Quality Control Data Sheet (BETA).xls").Activate
Application.ScreenUpdating = True
    
    If Range("ProductCode") = "10126" Or Range("ProductCode") = "10478" Or _
       Range("ProductCode") = "10561" Or Range("ProductCode") = "100041" Or _
       Range("ProductCode") = "100900" Or Range("ProductCode") = "100977" Or _
       Range("ProductCode") = "101901" Or Range("ProductCode") = "101949" Or _
       Range("ProductCode") = "102077" Or Range("ProductCode") = "102109" Or _
       Range("ProductCode") = "102131" Or Range("ProductCode") = "102282" Or _
       Range("ProductCode") = "102951" Or Range("ProductCode") = "7011837" Or _
       Range("ProductCode") = "7012112" Or Range("ProductCode") = "10073-U" Then
           MsgBox "The FILM for this Product Code can be tested every 2 hours, but has to be approved by KYLE CLAY or RICK KNIGHT.", vbInformation
    End If
    
    If Range("ProductCode") = "110080" Or Range("ProductCode") = "11078" Or _
       Range("ProductCode") = "11078-R" Or Range("ProductCode") = "111818" Or _
       Range("ProductCode") = "112147" Or Range("ProductCode") = "7180674" Or _
       Range("ProductCode") = "7188175" Or Range("ProductCode") = "7188335" Or _
       Range("ProductCode") = "7188338" Or Range("ProductCode") = "7188763" Or _
       Range("ProductCode") = "511024" Or Range("ProductCode") = "111391" Or _
       Range("ProductCode") = "11070" Then
        Chips.Show
    End If
    
    
    
Unload Me
LotNumber.Show
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.
The code works great when only numbers are being used but when a letter is introduced into the cell, I get a RUNTIME ERROR '13', TYPE MISMATCH debug error

Correct. As you've declared the 'CAFval' variable as a double type, it can only accept numeric entries being assigned to it. If the variable is to have both numeric values and text strings assigned to it, define the variable as a variant instead, i.e.

Code:
Dim CAFval As Variant

HTH

Robert
 
Last edited:
Upvote 0
This should not be over two lines:

Code:
Let CAFval = Workbooks("Quality Control Data Sheet (BETA).xls").Sheets("Data Sheet").Range("ProductCode").Value
 
Upvote 0
AMAS, Thanks for the reply. I know it is not suppose to be on two lines. The code was working with everything but letters,I must have hit enter when highlighting the line. Again,thanks for the reply.
 
Upvote 0
Trebor, may I ask what the difference is between using double and variant? I know the byte size is different anything else? I'm new to the "dim" concept. This code was actually one of my first with "dim"
 
Upvote 0
Double variables can only hold (be assigned) negative numbers -1.79769313486232E308 to -4.94065645841247E-324 or 4.94065645841247E-324 to 1.79769313486232E308 for positive values.

Variant variables though much more flexible in that they can hold (be assigned) numeric, string, date and the special values Empty and Null, they are more expensive in programming terms to use as their flexiblity comes at a cost in that they use more memory.

Dave Hawley has a great overview of variables, their types and uses here
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

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