Hello all,
I have been asked to embed logic into a macro enabled spreadsheet that will save a file with a standardized name ("projectmmddyyyy.xlsx") -- that works fine and well if we only have one version of this report on a given date, but if we have 2 or more, we need to append a differentiating character to the file name (b,c,d etc.)
To that end, I have worked out this block of code which scans the folder where this will be saved to look for a pre-existing report with the standard filename for that date, and if so, it will append the character:
This is failing at the "ActiveWorkbook.SaveAs Filename:=strPath & strFile, FileFormat _ :=xlOpenXMLWorkbook, CreateBackup:=False" block, and I cannot for the life of me figure out why. Can anyone shed any insight on that? Additionally, is there a better way to handle this on the whole? I realize the way I coded this will only account for the standard & standardB filename, so I will need to add additional steps to account for more records.
Thanks in advance!
Mike
I have been asked to embed logic into a macro enabled spreadsheet that will save a file with a standardized name ("projectmmddyyyy.xlsx") -- that works fine and well if we only have one version of this report on a given date, but if we have 2 or more, we need to append a differentiating character to the file name (b,c,d etc.)
To that end, I have worked out this block of code which scans the folder where this will be saved to look for a pre-existing report with the standard filename for that date, and if so, it will append the character:
VBA Code:
Sub Test1()
Dim strPath As String
Dim dtDate As Date
Dim strFileName As String
Dim strFileExists As String
Dim strFile As String
strPath = "U:\Test\"
dtDate = Date
strFileName = ("U:\Test\Project" & Format(dtDate, "mmddyyyy") & ".xlsx")
strFileExists = Dir(strFileName)
If strFileExists = "" Then
strFile = ("U:\Test\Project" & Format(dtDate, "mmddyyyy") & ".xlsx")
Else
strFile = ("U:\Test\Project" & Format(dtDate, "mmddyyyy") & "B.xlsx")
End If
ActiveWorkbook.SaveAs Filename:=strPath & strFile, FileFormat _
:=xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = False
End Sub
This is failing at the "ActiveWorkbook.SaveAs Filename:=strPath & strFile, FileFormat _ :=xlOpenXMLWorkbook, CreateBackup:=False" block, and I cannot for the life of me figure out why. Can anyone shed any insight on that? Additionally, is there a better way to handle this on the whole? I realize the way I coded this will only account for the standard & standardB filename, so I will need to add additional steps to account for more records.
Thanks in advance!
Mike