kwooden0626
New Member
- Joined
- Oct 15, 2016
- Messages
- 20
Hello All,
I have a VBA that will save the current workbook to a folder for the current year and month. It will first check to see if the folder already exists and if it doesn't it will create it prior to saving. My problem is that I created my folder structure using the month number and name so that the folders sort in the correct month order (i.e. 01. January, 02. February, etc.).
I tried to tweak this code by using MonthNumber(Month(Date) but this did not work. I receive an error stating "Sub or funtion not defined" and it highlights "MonthNumber."
Below is the full code I tried using.
If Len(Dir("C:\Documents\01 Leidos\01 Admin Ops\01 Daily Operations\01. Reports\04 Contractor PERSTAT", vbDirectory)) = 0 Then
MkDir "C:\Documents\01 Leidos\01 Admin Ops\01 Daily Operations\01. Reports\04 Contractor PERSTAT"
End If
' 'Save Report Part 2: Check for year folder and create if needed
If Len(Dir("C:\Documents\01 Leidos\01 Admin Ops\01 Daily Operations\01. Reports\04 Contractor PERSTAT" & Year(Date), vbDirectory)) = 0 Then
MkDir "C:\Documents\01 Leidos\01 Admin Ops\01 Daily Operations\01. Reports\04 Contractor PERSTAT" & Year(Date)
End If
' 'Save Report Part 3: Check for month folder and create if needed
If Len(Dir("C:\Documents\01 Leidos\01 Admin Ops\01 Daily Operations\01. Reports\04 Contractor PERSTAT" & Year(Date) & MonthNumber(Month(Date) & MonthName(Month(Date), False), vbDirectory))) = 0 Then
MkDir "C:\Documents\01 Leidos\01 Admin Ops\01 Daily Operations\01. Reports\04 Contractor PERSTAT" & Year(Date) & "" & MonthNumber(Month(Date) & MonthName(Month(Date), False))
End If
' 'Save Report Part 4: Save File
ActiveWorkbook.SaveAs Filename:= _
"C:\Documents\01 Leidos\01 Admin Ops\01 Daily Operations\01. Reports\04 Contractor PERSTAT" & Year(Date) & "" & MonthName(Month(Date), False) & "" & "Contractor PERSTAT-" & Format(Now, "dd-mmm-yyyy") & ".xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Function