Type mismatch error

Prepost

New Member
Joined
Dec 3, 2020
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I am new to vba and trying to code to select range in the excel file but getting type mismatch error for ' set myRange = Range("C:C") '. Can anyone help to fix this error.

'select Excel File

FPath = CATIA.FileSelectionBox("C:\Users\pughade\Desktop\DMU folder\PRIME\New folder", _
"*.xlsx", CatFileSelectionModeOpen)



If FPath = "" Then

Exit Sub

End If



Set xlApp = CreateObject("Excel.Application")

Set MyXL = GetObject(, "Excel.Application")

Set MyXL = GetObject(FPath)

MyXL.Application.Visible = True

MyXL.Parent.Windows(1).Visible = True


Set myRange = Range("C:C")


PILC = MyXL.Application.WorksheetFunction.CountA(myRange)



MsgBox ("Part Inst List Count=" & PILC)
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I don't see any declarations in your code but type mismatch usually means that you have used the wrong one.
VBA Code:
Dim myRange As Range
Set myRange = Range("C:C")
Declaring myRange as anything other than Range would cause a type mismatch. It should work if no declaration has been made, but that is widely considered as bad practice.
 
Upvote 0
I don't see any declarations in your code but type mismatch usually means that you have used the wrong one.
VBA Code:
Dim myRange As Range
Set myRange = Range("C:C")
Declaring myRange as anything other than Range would cause a type mismatch. It should work if no declaration has been made, but that is widely considered as bad practice.
Hi jasonb75, I have . see below

Sub CATMain()



Dim productDocument1 As ProductDocument

Set productDocument1 = CATIA.ActiveDocument


Dim selection1 As Selection

Set selection1 = productDocument1.Selection


Dim i As Integer

Dim PILC As Integer

Dim PI As String

Dim myRange 'As Range

Dim FPath

Dim xlApp

Dim MyXL

Dim selobj1 As Object

Set selobj1 = selection1.Selection

selection1.Clear


'select Excel File

FPath = CATIA.FileSelectionBox("C:\Users\pughade\Desktop\DMU folder\PRIME\New folder", _
"*.xlsx", CatFileSelectionModeOpen)


If FPath = "" Then

Exit Sub

End If


Set xlApp = CreateObject("Excel.Application")

Set MyXL = GetObject(, "Excel.Application")

Set MyXL = GetObject(FPath)

MyXL.Application.Visible = True

MyXL.Parent.Windows(1).Visible = True


Set myRange = Range("C:C")


PILC = MyXL.Application.WorksheetFunction.CountA(myRange)


MsgBox ("Part Inst List Count=" & PILC)
 
Upvote 0
Where is your VBA code located?

Could you please put code tags around your code? It would make it a lot easier to read.
 
Upvote 0
Where is your VBA code located?

Could you please put code tags around your code? It would make it a lot easier to read.
I am writing this to drive Catia from excel file. I am running it through Catia platform
 
Upvote 0
The apostrophe in the middle of the line,
VBA Code:
Dim myRange 'As Range
is causing it to be declared as variant instead of range, which is not ideal but should still work.

It would be preferable to use Long instead of Integer for a couple of your other declarations but that will not be the cause of the problem you're seeing.

Try qualifying the range with a workbook and worksheet name rather than just setting the range. Catia is not something that I'm familiar with but I'm wondering if the code is looking in the wrong place and not seeing the range.
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,118
Members
449,066
Latest member
Andyg666

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