Good day,
I am trying to open an archived workbook in the "Archive" folder to copy contents of a worksheet to a new workbook with the same sheet name.
There are multiple files in folder and the opened file depends on the customer name. (there will be multiple customers and multiple workbooks with a single customer)
The file names are all structured as PREFIX (FSO or PPG), Ticket Number (111234), Customer Name, Job Type and Short Date, like, "FSO 111234 Co Name Job Type 022822.xlsm"
My issue is that the following code opens the first file in the folder and not the newest file. I require the newest file from the wild carded customer name.
Public Sub Archive_Ticket_Fill()
Dim xpath, xfile, LatestFile As String
Dim InternalWB, wb As Workbook
Dim LatestDate, LMD As Date
Set wb = Nothing
xpath = "C:\Start\Archive\"
If Right(xpath, 1) <> "\" Then xpath = xpath & "\"
xfile = Dir$(xpath & "*" & Worksheets("DM").Range("D10").Value & "*.xlsm", vbNormal)
If Len(xfile) = 0 Then Exit Sub
Do While Len(xfile) > 0
LMD = FileDateTime(xpath & xfile)
If LMD >= LatestDate Then
LatestFile = xfile
LatestDate = LMD
End If
xfile = Dir
Loop
On Error Resume Next
Set wb = Workbooks(LatestFile)
On Error GoTo 0
If wb Is Nothing Then
Set InternalWB = Workbooks.Open(xpath & LatestFile)
'COPY CODE HERE
Else
MsgBox LatestFile & " is already open."
'COPY CODE HERE
End If
End Sub
Any suggestions or help is appreciated. Thank you for your time.
Related
I update a spreadsheet three times a week that summarises business deliveries and other information.
The sheet requires me to dump in three or four intake reports each time to lookup the relevant data. I want to open the most recent file in a folder and copy the data into my active workbook.
I can't open the file. I get a run time error saying the file/path can't be found.
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 = "C:\Users\Documents"
'Make sure that the path ends in a backslash
If Right(Mypath, 1) <> "\" Then Mypath = Mypath & "\"
'Get the lfirst excel file from the folder
Myfile = Dir(Mypath & "*xlsx", 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 folder
Do While Len(Myfile) > 0
'If 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
The reason is simple: You never assign anything to LMD, so LMD is always 0 (that is the initial value for a date). As a consequence, LMD is never > LatestDate (which is also 0), you never assign any value to Myfile. At the end, you try to open a file with the name of your folder and that of course fails.
Simply add the FileDateTime command to fetch the file date:
LMD = FileDateTime(Mypath & Myfile)
If LMD > LatestDate Then
LatestFile = Myfile
LatestDate = LMD
End If
Hint: Learn to use the VBA debugger to check for such problems. I recommend to watch https://www.youtube.com/watch?v=Um2JwZfwoFI, but you can find a lot of other resources.
I pulled some code from online to open the latest file in a folder, which seems to work well. Later in the code however, I added an additional line to SET that same, recent file which was opened. The Workbook subscipt is out range when trying this and I think it has something to do with the syntax? May need to incorporate additional quotes into the workbook name, any ideas?
The 'path' and 'latestFile' variables seem to be reading in correctly
Dim path$, file$, latestFile$
Dim LatestDate As Date, LMD As Date
Dim D As worksheet, dash As worksheet
'open latest file
path = "R:\Dept\"
If Right(path, 1) <> "\" Then path = path & "\"
file = Dir(path & "*.xls", vbNormal)
If Len(file) = 0 Then
MsgBox "No files were found in the folder", vbExclamation
End If
Do While Len(file) > 0
LMD = FileDateTime(path & file)
If LMD > LatestDate Then
latestFile = file
LatestDate = LMD
End If
file = Dir
Loop
Application.DisplayAlerts = False
Workbooks.Open path & latestFile
Application.DisplayAlerts = True
Set dash = Workbooks("dashboard.xlsm").Worksheets("D data")
Set D = Workbooks(path & latestFile).Worksheets("D Data") 'error here
Just use the name:
Set D = Workbooks(latestFile).Worksheets("D Data")
But cleaner to do this:
Application.DisplayAlerts = False
Set D = Workbooks.Open(path & latestFile).Worksheets("D Data") '<< edit
Application.DisplayAlerts = True
I am wanting to write a VBA code to import the newest .csv file from a certain directory into a specific excel sheet. I am currently using a code that works, BUT it opens up in a new workbook and I cannot figure out how to append it to a certain sheet instead. If that makes sense? Also, is there a way to also delete all duplicates within that specific sheet after imported?
The code I am currently using through VBA is below....
Option Explicit
Sub NewestFile()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "C:\Users\Documents\"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xls", 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
The issue is that this code opens up in a new workbook instead of importing into a specific sheet.
I have a folder that during the day can be updated with files named according to these patterns ("D0_yyyymmdd.xlsx") and ("D1_yyyymmdd"). So, for example we can find "D0_20200506" and "D1_20200506".
I am writing a program where depending on the user request ("D0" or "D1") I have to get the information of the latest version available in the folder. I already have a code that works for openning the latest file, but the code does not respect the user request "D0" or "D1". Having said that, if the user request is "D0" I have to get information from the latest version of files that follow the "D0_yyyymmdd.xlsx" pattern.
Can someone help me?
'Force the explicit declaration of variables
Option Explicit
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
Dim closedBook As Object
Dim GetBookName As String
GetBookName = ActiveWorkbook.Name
'Specify the path to the folder
MyPath = "\\testingcode\"
'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 & "*.xlsx", 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
Application.ScreenUpdating = False
Workbooks.Open MyPath & LatestFile
Sheets(1).Copy After:=Workbooks(GetBookName).Sheets(Sheets.Count)
Workbooks(LatestFile).Close SaveChanges:=False
Application.ScreenUpdating = True
Choose Either Using Buttons
For CommandButton1 and CommandButton2 change their Caption to D0 and D1. Then use the following in the Sheet Code e.g. Sheet1:
Option Explicit
Private Sub CommandButton1_Click()
OpenLatestFile Me.CommandButton1.Caption
End Sub
Private Sub CommandButton2_Click()
OpenLatestFile Me.CommandButton2.Caption
End Sub
Now make the sub 'argumented' in the following way:
Sub OpenLatestFile(CmdCaption As String)
Change the appropriate line by doing the following:
If the filename starts with D0 or D1 use:
'Get the first Excel file from the folder
MyFile = Dir(MyPath & CmdCaption & "*.xlsx", vbNormal)
If the filename only contains D0 or D1 use:
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*" & CmdCaption & "*.xlsx", vbNormal)
The sub should stay in a standard module e.g. Module1.
I have code that opens up the latest workbook file in a specified folder.
The macro I have setup to pull information from that workbook file won't work week to week. The macro references the workbook file which is named 2019-09-16 but next week the workbook file will be 2019-09-20.
If I could save a copy of the workbook in another folder under a generic name for example Archive that would allow me to get information from that workbook.
I attempted type the code I had for opening the latest file to help link something together with the macro but I got many errors.
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
MyPath = "\\C\s\CAF7\Stats\Team 1\Archive\"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
Windows("Teams.xlsm").Activate
Range("F11").Select
ActiveCell.FormulaR1C1 = "='[Workbooks.Open MyPath & LatestFile]Managers Sheet'!R33C13"
Range("E12").Select
ActiveCell.FormulaR1C1 = "='[Workbooks.Open MyPath & LatestFile]Managers Sheet'!R24C13"
Range("D9").Select
ActiveCell.FormulaR1C1 = "='[Workbooks.Open MyPath & LatestFile]Managers Sheet'!R38C13"
Range("F13").Select
ActiveCell.FormulaR1C1 = "='T1'!R[7]C"
Range("F14").Select
ActiveCell.FormulaR1C1 = "='T1'!R[20]C"
Range("F15").Select
ActiveCell.FormulaR1C1 = "='T1'!R[33]C"
Range("F16").Select
ActiveCell.FormulaR1C1 = "='t1'!R[19]C[2]"
Range("F17").Select
ActiveCell.FormulaR1C1 = "='t1'!R[18]C[8]"
Range("E18:F27").Select
End Sub
This is what I have tried. The workbook opens but when trying to run the macro it tries to find the workbook from the reference and can't locate.
If there is only one sheet in the file (with the currently updated information) then simply refer to the sheet by its index
TheLatestFile.Worksheets(1)
If there are multiple sheets and you can definitively know where it is in the workbook tab order you can use that known index position instead.
TheLatestFile.Worksheets(KnownIndex)
If there are multiple sheets and you cannot know its position in the tab order you can get the correct sheet from the codename if you can ensure the codename is consistent. This approach requires the codename not change from week to week (updated file to updated file) AND you will need to Trust Access to the VBA Project model.