VBA: Save as new file name on dedicated folder

josephmary

New Member
Joined
Oct 18, 2023
Messages
27
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: 6

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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