Stumped - Type Mismatch

kwhite100

Board Regular
Joined
Aug 18, 2010
Messages
91
Hello,

I am a little stumped on an issue that I am facing. I am simply trying to open a second workbook and am getting a type mismatch error. I can't figure out the issue and have spent hours debugging and searching around the internet. It seems to be whenever I try to load a 2nd file, no matter which order.

Thanks for the look!

Code:
Sub Test()
Dim fname, ename, fpath, epath As Variant
Dim intChoicef, intchoicee As Integer
Dim fWorkBook, eWorkBook As Workbooks
With Workbooks("KPRO Summary Test 2")
fpath = Application.GetOpenFilename
If fpath <> False Then
    fname = Mid(fpath, InStrRev(fpath, "\") + 1, (Len(fpath) - InStrRev(fpath, "\", 1)))
        Workbooks("KPRO Summary Test 2").Sheets("Sheet1").Cells(1, 3).Value = "[" & fname & "]KPRO SUMMARY"
    Set fWorkBook = Workbooks.Add(fpath)
    Else
    MsgBox "Please select a file"
End If
epath = Application.GetOpenFilename
If epath <> False Then
    ename = Mid(epath, InStrRev(epath, "\") + 1, (Len(epath) - InStrRev(epath, "\", 1)))
        Workbooks("KPRO Summary Test 2").Sheets("Sheet1").Cells(2, 3).Value = "[" & ename & "]CCIR Tracker"
    Set eWorkBook = Workbooks.Add(epath)
    Else
    MsgBox "Please select a file"
End If
End With
Workbooks("KPRO Summary Test 2").Activate
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
...I am simply trying to open a second workbook and am getting a type mismatch error...

I did not test, but you probably want to be declaring your variables like:

Code:
Dim fname As Variant
Dim ename As Variant
'etc
Dim fWorkBook As Workbook
Dim eWorkBook As Workbook

See, each variable must have 'As [some type]' after it, else it is just a Variant. In this case, you also want to declare the opening workbook (the one you are setting a reference to) 'As Workbook', not 'Workbooks', which would refer to the Workbooks Collection instead of an individual Workbook Object. Does that make sense?

Finally, you probably want to use the Workbooks.Open method, rather than the .Add method.

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

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