Smurphster16
New Member
- Joined
- Feb 28, 2019
- Messages
- 25
Hi guys I have the following code which allows me to open the latest file in my specified folder which contains the wildcard i specify - however the code only works when i hardcode the wildcard entry. But i want the wildcard to be a variable so that I can use a loop to open several most recent files in the folder - each containing a different wildcard.
In the code below I wanted to set c as the variable in the excel sheet and use this as the wildcard, bu instead of using the value i set for C it uses C itself
Any help on this would be much appreciated,
Thanks
In the code below I wanted to set c as the variable in the excel sheet and use this as the wildcard, bu instead of using the value i set for C it uses C itself
Any help on this would be much appreciated,
Thanks
VBA Code:
Sub NewestFile()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
C = Sheets("Sheet1").Range("OBR")
MyPath = Sheets("Sheet1").Range("Desired_Path")
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*C *.csv", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
End Sub