dougmarkham
Active Member
- Joined
- Jul 19, 2016
- Messages
- 252
- Office Version
- 365
- Platform
- Windows
Hi Folks,
I have found a great macro which allows you to check for the existence of folders in a file-path matching: Year, Month and Day (i.e., creating the folder if it doesn't exist).
For instance, if the FilePath starts: C:\Temp Time Sheets\
it checks for
C:\Temp Time Sheets\2019\
C:\Temp Time Sheets\2019\11_November\
C:\Temp Time Sheets\2019\11_November\21\
...creating a folder for any missing folders in the above.
My aim is for the last folder in the FilePath to be a date-range spanning the current week e.g., for this week: \16.11.2019-22.11.2019\ (i.e., the folder name would contain two parts: 1) the date for Monday and 2) the date for Friday separated by a hyphen or something similar).
So the full FilePath would be C:\Temp Time Sheets\2019\11_November\16.11.2019-22.11.2019\
I know that to populate a cell with the date for a Monday of any given week, I need to input: =TODAY() - WEEKDAY(TODAY(),3)
However, using the following:
...returns the error message: Sub or Function not defined
I replaced the TODAY element with NOW, and got "Run-time error '52: Bad file name or number"
Would anybody be willing to help me modify the code to achieve this aim?
Kind regards,
Doug.
I have found a great macro which allows you to check for the existence of folders in a file-path matching: Year, Month and Day (i.e., creating the folder if it doesn't exist).
For instance, if the FilePath starts: C:\Temp Time Sheets\
it checks for
C:\Temp Time Sheets\2019\
C:\Temp Time Sheets\2019\11_November\
C:\Temp Time Sheets\2019\11_November\21\
...creating a folder for any missing folders in the above.
VBA Code:
Sub DateFolderSave()
Dim strGenericFilePath As String: strGenericFilePath = "C:\Temp Time Sheets\"
Dim strYear As String: strYear = Year(Date) & "\"
Dim strMonth As String: strMonth = Format(Month(Date), "00") & "_" & MonthName(Month(Date)) & "\"
Dim strDay As String: strDay = Day(Date) & "\"
Dim strFileName As String: strFileName = "Time Sheet"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlManual
ThisWorkbook.CheckCompatibility = False
' Check for year folder and create if needed
If Len(Dir(strGenericFilePath & strYear, vbDirectory)) = 0 Then
MkDir strGenericFilePath & strYear
End If
' Check for month folder and create if needed
If Len(Dir(strGenericFilePath & strYear & strMonth, vbDirectory)) = 0 Then
MkDir strGenericFilePath & strYear & strMonth
End If
' Check for date folder and create if needed
If Len(Dir(strGenericFilePath & strYear & strMonth & strDay, vbDirectory)) = 0 Then
MkDir strGenericFilePath & strYear & strMonth & strDay
End If
' Save File
ActiveWorkbook.SaveAs fileName:= _
strGenericFilePath & strYear & strMonth & strDay & strFileName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ThisWorkbook.CheckCompatibility = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
' Popup Message
MsgBox "File Saved As: " & vbNewLine & strGenericFilePath & strYear & strMonth & strDay & strFileName
End Sub
My aim is for the last folder in the FilePath to be a date-range spanning the current week e.g., for this week: \16.11.2019-22.11.2019\ (i.e., the folder name would contain two parts: 1) the date for Monday and 2) the date for Friday separated by a hyphen or something similar).
So the full FilePath would be C:\Temp Time Sheets\2019\11_November\16.11.2019-22.11.2019\
I know that to populate a cell with the date for a Monday of any given week, I need to input: =TODAY() - WEEKDAY(TODAY(),3)
However, using the following:
Code:
Dim strDay As String: strDay = TODAY() - Weekday(TODAY(), 3) & "-" & TODAY() - Weekday(TODAY(), 3) + 4 & "\"
...returns the error message: Sub or Function not defined
I replaced the TODAY element with NOW, and got "Run-time error '52: Bad file name or number"
Would anybody be willing to help me modify the code to achieve this aim?
Kind regards,
Doug.