VBA: Save as new file name on dedicated folder

josephmary

New Member
Joined
Oct 18, 2023
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hi,

I wanted to create a code where my file is being saved as CSV with a file name that also changes every month. I feel like my code is correct as it saves perfectly. However, I encountering an error when i open that saved file (See screenshot)
Here's the VBA code:
SUB save
ChDir _
"C:\Users\" & Environ("username") & "\Documents\Reports\QA Report\GCP"
Dim File_Name As String
File_Name = Format(Now(), "yyyy-mm") & " QAAttributes" & ".csv"
ActiveWorkbook.SaveAs Filename:=File_Name
END
 

Attachments

  • excelerror.JPG
    excelerror.JPG
    22.4 KB · Views: 4

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
try this,

VBA Code:
Sub save()

Dim File_Name As String

myPath = "C:\Users\" & Environ("username") & "\Documents\Reports\QA Report\GCP\"
    If Dir(myPath, vbDirectory) = "" Then Shell ("cmd /c mkdir """ & myPath & """")
ChDir myPath
File_Name = Format(Now(), "yyyy-mm") & " QAAttributes" & ".csv"
ActiveWorkbook.SaveAs Filename:=File_Name
End Sub
 
Upvote 0
Is there another option without running cmd? we're not allowed to open cmd as part of our work policy 😅
 
Upvote 0
Add the FileFormat property to your SaveAs method
VBA Code:
SUB save()
ChDir _
"C:\Users\" & Environ("username") & "\Documents\Reports\QA Report\GCP"
Dim File_Name As String
File_Name = Format(Now(), "yyyy-mm") & " QAAttributes" & ".csv"
ActiveWorkbook.SaveAs Filename:=File_Name, FileFormat:=xlCSV
END sub
 
Upvote 1
Solution
Good day Skyybot or anyone. This is almost similar to my question above. but this time, i want my file to be saved on a different folder every month. (eg. I want my november file to be saved on the november folder).
 
Upvote 0
Good day Skyybot or anyone. This is almost similar to my question above. but this time, i want my file to be saved on a different folder every month. (eg. I want my november file to be saved on the november folder).
Try this
VBA Code:
Sub SaveToFolder()
Dim File_Name As String
Dim Folder_Name As String
ChDir "C:\Users\" & Environ("username") & "\Documents\Reports\QA Report\GCP"
Folder_Name = Format(Now(), "yyyy-mm")
MkDir CurDir & "\" & Folder_Name
ChDir CurDir & "\" & Folder_Name
File_Name = Format(Now(), "yyyy-mm") & " QAAttributes" & ".csv"
ActiveWorkbook.SaveAs Filename:=File_Name, FileFormat:=xlCSV
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,092
Messages
6,123,063
Members
449,090
Latest member
fragment

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top