Compile error: Type Mismatch

EdwardL95

New Member
Joined
Aug 17, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi All,

When trying to put an array in a IF = False then statement it is giving me a "Compile error Type Mismatch" error. Does anyone have an Idea?, I can get around this by removing the If statement or array...

Code below:
VBA Code:
Dim Viia7Files() As Variant
Dim wb As Workbook
Dim wb2 As Workbook
Dim I As Integer

Set wb = ThisWorkbook
    
Viia7Files = Application.GetOpenFilename("Excel Files (*.xls*),*.xl*", , "Choose File", "Open", True)



If Viia7Files = False Then Exit Sub
    
Else
    
    For I = 1 To Application.CountA(Viia7Files)

    Set wb2 = Workbooks.Open(Viia7Files(I))
    
End If

Thanks,
Ed
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try explaining what you're trying to do, there are too many errors in your code for anyone to make sense of it.
 
Upvote 0
Just guessing ...
VBA Code:
Sub EdwardL95()

    Dim Viia7Files As Variant
    Dim wb As Workbook
    Dim wb2 As Workbook
    Dim I As Long

    Set wb = ThisWorkbook
    
    Viia7Files = Application.GetOpenFilename("Excel Files (*.xls*),*.xl*", , "Choose File", "Open", True)

    If VarType(Viia7Files) = vbBoolean Then
        Exit Sub
    
    Else
        For I = 1 To UBound(Viia7Files)
            Set wb2 = Workbooks.Open(Viia7Files(I))
        Next I
    End If
End Sub
 
Upvote 0
Try explaining what you're trying to do, there are too many errors in your code for anyone to make sense of it.


Apologies I'm still new to this.

What I wanted to do was add in the section below however I was getting the mismatach error and If I were to remove it the code works.

VBA Code:
If Viia7Files = False Then

MsgBox ("No File Selected")
    
    Else

Full code is below:

VBA Code:
Sub ImportViia7File()

Dim Viia7Files() As Variant
Dim wb As Workbook
Dim wb2 As Workbook
Dim I As Long

Set wb = ThisWorkbook
    
Viia7Files = Application.GetOpenFilename("Excel Files (*.xls*),*.xl*", , "Choose File", "Open", True)


If Viia7Files = False Then

MsgBox ("No File Selected")
    
    Else
    
End If

For I = 1 To Application.CountA(Viia7Files)

    Set wb2 = Workbooks.Open(Viia7Files(I))
    

wb.Activate
wb.Worksheets.Add(Before:=Sheets("Worklist_Template")).Name = "Viia7Data_" & I
    
wb2.Activate
wb2.Sheets("Results").Range("D43:E1195").Copy wb.Sheets("Viia7Data_" & I).Range("A1")
wb2.Sheets("Results").Range("G43:G1195").Copy wb.Sheets("Viia7Data_" & I).Range("C1")
wb2.Sheets("Results").Range("I43:I1195").Copy wb.Sheets("Viia7Data_" & I).Range("D1")

Application.CutCopyMode = False
wb2.Close

wb.Activate

Next I


End Sub
 
Upvote 0
With this line of code
VBA Code:
Dim Viia7Files() As Variant
you're explicitly declaring a variable of the Variant/Array vartype. Remove the parenthesis, then it would be a generic Variant vartype and can then take any type.
The GetOpenFileName function returns either an Array vartype (one ore multiple files selected by user) or a Boolean vartype (cancel pressed).
 
Upvote 0
Solution
With this line of code
VBA Code:
Dim Viia7Files() As Variant
you're explicitly declaring a variable of the Variant/Array vartype. Remove the parenthesis, then it would be a generic Variant vartype and can then take any type.
The GetOpenFileName function returns either an Array vartype (one ore multiple files selected by user) or a Boolean vartype (cancel pressed).
Thank you.
 
Upvote 0

Forum statistics

Threads
1,216,759
Messages
6,132,556
Members
449,735
Latest member
Gary_M

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