Hi All
Here is my problem.
I have an excel file which every month i want to open and it to contain new data from from the latest txt file in a folder.
So every month a new txt file is output to this folder. The folder could contain up to 12 txt files at anyone time all named differently like Dec14, Jan15 etc.
When i open my excel file i want it to update and pull the data in from the most recent txt file.
I know i could create a link to a file which would update on open (problem is the names change each month)
I also have the VB code to open the most recent txt file in the folder but that opens it into a new document and does not delimit it.
Below is the VB Code im using
Sub OpenLatestFile()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
'Specify the path to the folder
MyPath = "S:\Claims Analytics Reporting\Project_Opal\UK Extracts\2.1.1\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.Txt", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
'Open the latest file
Workbooks.Open MyPath & LatestFile
End Sub
Any help is much appreciated
Here is my problem.
I have an excel file which every month i want to open and it to contain new data from from the latest txt file in a folder.
So every month a new txt file is output to this folder. The folder could contain up to 12 txt files at anyone time all named differently like Dec14, Jan15 etc.
When i open my excel file i want it to update and pull the data in from the most recent txt file.
I know i could create a link to a file which would update on open (problem is the names change each month)
I also have the VB code to open the most recent txt file in the folder but that opens it into a new document and does not delimit it.
Below is the VB Code im using
Sub OpenLatestFile()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
'Specify the path to the folder
MyPath = "S:\Claims Analytics Reporting\Project_Opal\UK Extracts\2.1.1\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.Txt", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
'Open the latest file
Workbooks.Open MyPath & LatestFile
End Sub
Any help is much appreciated