Open File name with changing date in name

Plawrence

New Member
Joined
Dec 10, 2020
Messages
7
Office Version
  1. 2010
Platform
  1. Windows
I’m trying to get a vba to open a file daily with a macros and am having trouble doing so. The file is in windows and it’s listed like this “gville grade sheet_12-5-2020” the name stays the same but the date changes.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
So, that one is 12/5/2020. Would you be running this on 12/6/2020 and looking for the previous date? Or are you wanting to just open the most recent file in the folder?
 
Upvote 0
So, that one is 12/5/2020. Would you be running this on 12/6/2020 and looking for the previous date? Or are you wanting to just open the most recent file in the folder?
The most recent, it would be imported once daily.
 
Upvote 0
How about this?

VBA Code:
Sub OpenFile()
Dim Path As String:     Path = "C:\Users\User\Desktop" 'Change to your folder
Dim FSO As Object:      Set FSO = CreateObject("Scripting.FileSystemObject")
Dim Fol As Object:      Set Fol = FSO.getfolder(Path)
Dim b As Boolean:       b = True
Dim eDate As Date

For Each fil In Fol.Files
    If fil.Name Like "gville grade sheet" Then
        If b Then
            eDate = fil.datecreated
            Path = fil.Path
            b = False
        End If
        If fil.datecreated > eDate Then
            eDate = fil.datecreated
            Path = fil.Path
        End If
    End If
Next fil

Workbooks.Open (Path)

End Sub
 
Upvote 0
How about this?

VBA Code:
Sub OpenFile()
Dim Path As String:     Path = "C:\Users\User\Desktop" 'Change to your folder
Dim FSO As Object:      Set FSO = CreateObject("Scripting.FileSystemObject")
Dim Fol As Object:      Set Fol = FSO.getfolder(Path)
Dim b As Boolean:       b = True
Dim eDate As Date

For Each fil In Fol.Files
    If fil.Name Like "gville grade sheet" Then
        If b Then
            eDate = fil.datecreated
            Path = fil.Path
            b = False
        End If
        If fil.datecreated > eDate Then
            eDate = fil.datecreated
            Path = fil.Path
        End If
    End If
Next fil

Workbooks.Open (Path)

End Sub
Looks good, will try and let you know thanks!
 
Upvote 0
Looks good, will try and let you know thanks!
How about this?

VBA Code:
Sub OpenFile()
Dim Path As String:     Path = "C:\Users\User\Desktop" 'Change to your folder
Dim FSO As Object:      Set FSO = CreateObject("Scripting.FileSystemObject")
Dim Fol As Object:      Set Fol = FSO.getfolder(Path)
Dim b As Boolean:       b = True
Dim eDate As Date

For Each fil In Fol.Files
    If fil.Name Like "gville grade sheet" Then
        If b Then
            eDate = fil.datecreated
            Path = fil.Path
            b = False
        End If
        If fil.datecreated > eDate Then
            eDate = fil.datecreated
            Path = fil.Path
        End If
    End If
Next fil

Workbooks.Open (Path)

End Sub
get a run time error 76 on Dim Fol As Object: Set Fol = FSO.getfolder(Path)
 
Upvote 0
Did you change the path variable?
Dim Path As String: Path = "X:\Gordonsville\Dispatch\Grade Sheet\Gville Grade Sheet"
Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim Fol As Object: Set Fol = FSO.getfolder(Path)
Dim b As Boolean: b = True
Dim eDate As Date

For Each fil In Fol.Files
If fil.Name Like "gville grade sheet" Then
If b Then
eDate = fil.datecreated
Path = fil.Path
b = False
End If
If fil.datecreated > eDate Then
eDate = fil.datecreated
Path = fil.Path
End If
End If
Next fil

Workbooks.Open (Path)


this is what i got....
 
Upvote 0
I don't know. The code works on this end. That's a 'path not found' error. I would make sure there are no typos in the path, but outside of that, I'm not sure.
 
Upvote 0
I don't know. The code works on this end. That's a 'path not found' error. I would make sure there are no typos in the path, but outside of that, I'm not sure.
thanks for your help I still cant get it to work, but thanks for trying...
 
Upvote 0

Forum statistics

Threads
1,215,426
Messages
6,124,829
Members
449,190
Latest member
rscraig11

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