VBA Run time error 13: Mismatch

kamisha05

New Member
Joined
Sep 25, 2015
Messages
8
Hello,

I am using the following subroutine and recently I have received a run time error stating type mismatch in regards to the line: Sep = Chr(9). I never had an issue with that line in the past. Any ideas?


Sub DoTheImport()


Dim shtName As String
Dim Chr
shtName = "Product " & CStr(ProdNum)

Dim Prod As Variant
Dim Sep As String

Application.ScreenUpdating = False


'Activate data sheet
Worksheets(shtName).Activate
Worksheets(shtName).Cells(1, 1).Activate


'Ask for file
Prod = Application.GetOpenFilename(FileFilter:="Input Files (*.csv),*csv")

'If no file selected, end macro
If Prod = False Then
Exit Sub
End If

Sep = Chr(9)
ImportProduct FName:=CStr(Prod), Sep:=Sep


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Welcome to the forum! Please paste code between code tags. Insert them by click the # icon on the reply message toolbar.

Use
Code:
Option Explicit
as the first line in a Module.

You could then see that you missed Dimming some variable. Do not use variable names where a command name already exists like Chr.

Code:
Sub DoTheImport()
  Dim shtName As String, Prod As Variant, Sep As String
  Dim ProdNum As Long
  
  ProdNum = 123
  shtName = "Product " & CStr(ProdNum)
  
  Application.ScreenUpdating = False
  
  'Activate data sheet
  Worksheets(shtName).Activate
  Worksheets(shtName).Cells(1, 1).Activate
  
  'Ask for file
  Prod = Application.GetOpenFilename(FileFilter:="Input Files (*.csv),*csv")
  
  'If no file selected, end macro
  If Prod = False Then Exit Sub
  
  Sep = Chr(9)
  'ImportProduct FName:=CStr(Prod), Sep:=Sep
End Sub
 
Upvote 0
Code:
Sub DoTheImport(ProdNum)

Dim shtName As String
Dim Chr
    shtName = "Product " & CStr(ProdNum)
    
    Dim Prod As Variant
    Dim Sep As String
    
Application.ScreenUpdating = False


'Activate data sheet
    Worksheets(shtName).Activate
    Worksheets(shtName).Cells(1, 1).Activate
    
    
'Ask for file
    Prod = Application.GetOpenFilename(FileFilter:="Input Files (*.csv),*csv")
    
'If no file selected, end macro
    If Prod = False Then
        Exit Sub
    End If
    
    Sep = Chr(9)
    ImportProduct FName:=CStr(Prod), Sep:=Sep
    
    
End Sub
 
Upvote 0
Also, I have ProdNum defined already in a public subroutine located in a different module within the workbook.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,202
Members
448,554
Latest member
Gleisner2

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