VBA SaveCopyAs with File Format question

mriveraCP

New Member
Joined
Jan 3, 2017
Messages
8
After reading threads, I have realized you cannot change the File Format using the SaveCopyAs Filename syntax. How do I get around this? I have created a command button that is creating a folder on my computer, saving my current macro enabled file as a .csv and everything is working...however, it changes the current workbook file name instead of creating a copy and it changes the tab name in the workbook to the new SaveAs file name. Any tips are much appreciated?

Here is my current code below:

Sub SaveCSV()
'
' SaveCSV Macro
'
ChDir "C:\VSA\ForecastComments"
ActiveWorkbook.SaveAs Filename:= _
"C:\VSA\ForecastComments\2017 FC Comments-" & Worksheets("Summary").Cells(1, 2).Text & " " & Worksheets("Summary").Cells(2, 2).Text & ".csv", FileFormat:= _
xlCSV, CreateBackup:=False
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi and welcome to MrExcel!
Try this code:
Rich (BB code):
Sub SaveAsCsv()
 
  Dim FName As String
 
  FName = "C:\VSA\ForecastComments\2017 FC Comments-" & Worksheets("Summary").Cells(1, 2).Text & " " & Worksheets("Summary").Cells(2, 2).Text & ".csv"
  
  ' Stop screen refreshing
  Application.ScreenUpdating = False
   
  ' Create a copy of active sheet in the new workbook
  ActiveSheet.Copy
 
  ' Save the new workbook as CSV and close it
  With ActiveWorkbook
    .SaveAs Filename:=FName, FileFormat:=xlCSV, CreateBackup:=False
    .Close False
  End With
 
  ' Return back to this workbook
  ThisWorkbook.Activate
 
  ' Unfreeze the screen
  Application.ScreenUpdating = True
 
End Sub
 
Last edited:
Upvote 0
Thank you Vladimir! When I copied & pasted the code above, it displayed an error message: "Run time error 9 , subscript out of range" and it highlighted the line starting with FName = "C:\VSA....etc. Am I missing a step? Thanks again!
 
Upvote 0
Actually, when I tried this again, it worked!!! Thank you for taking the time to help me out, I sincerely appreciate it!!!
 
Upvote 0
You are very welcome and happy coding! :)
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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