VBA code to open last file in folder not working

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all!

I have a macro that opens last modified file with partial name from specific folder. It returns mismatch error on
VBA Code:
 Set filToOpen = fil
line and I have no clue why, can somebody explain it please?



VBA Code:
Private Sub OpenMaster_Click()

Const sPartialName As String = "Master File"
Dim sStartPath As String
Dim sFileExt As String
Dim fso, fldr, fil, S, myDate, filToOpen As Workbook

sStartPath = "C:\Local\Raports\"
sFileExt = "*.xlsx"
Set fso = CreateObject("Scripting.Filesystemobject")
Set fldr = fso.getfolder(sStartPath).Files
For Each fil In fldr
    If fil.Name Like sPartialName & "*" & sFileExt Then
       If fil.datelastmodified > myDate Then
              myDate = fil.datelastmodified
             Set filToOpen = fil   'This line returns mismatch
       End If
End If
Next fil
If Not filToOpen Is Nothing Then
       Workbooks.Open Filename:=filToOpen.Name
Else
       MsgBox "No Master File found."
End If
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Following line requires filToOpen is declared as File, or as Object since you are using late binding.

VBA Code:
Set filToOpen = fil

However, it is declared as Workbook in the code:

VBA Code:
Dim fso, fldr, fil, S, myDate, filToOpen As Workbook

Replace it with Object.

VBA Code:
Dim fso, fldr, fil, S, myDate, filToOpen As Object

Not related with your question. However, ideally, also declare fso, fldr, fil variables as Object as well instead of Variant.
 
Upvote 0
Solution
Note that this line:
VBA Code:
Dim fso, fldr, fil, S, myDate, filToOpen As Workbook
only declare "filToOpen" as Workbook. The rest will be set to the default "Variant".

Each variable must be explicitly declared if you want to ensure they are a certain data type, i.e.
VBA Code:
Dim fil as Workbook, filToOpen As Workbook
etc
 
Upvote 0
Following line requires filToOpen is declared as File, or as Object since you are using late binding.

VBA Code:
Set filToOpen = fil

However, it is declared as Workbook in the code:

VBA Code:
Dim fso, fldr, fil, S, myDate, filToOpen As Workbook

Replace it with Object.

VBA Code:
Dim fso, fldr, fil, S, myDate, filToOpen As Object

Not related with your question. However, ideally, also declare fso, fldr, fil variables as Object as well instead of Variant.
Thank you, macro runs now without issues
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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