Export multiple worksheets as CSV with worksheet name

jpsimmon

New Member
Joined
Apr 9, 2015
Messages
28
What do I add to this code to export my worksheets with their worksheet name?
Currently it shoots them out as Sheet1, Sheet2, Sheet3 etc...

Code:
Sub ExpCsv()Dim i As Integer


For i = 1 To 3


Sheets(i).Activate
Range("A1:W645").Copy
Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs Filename:="S:\Com\Monthly\Test\" & Sheets(i).Name & ".csv", FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True


Next
End Sub

Thanks,
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Welcome to the Board!

I take it that you are wanting to capture the sheet names from your original file. The problem is that you are grabbing the sheet name from the new workbook you are adding, not your original file. Since they will all be "Sheet1", you will have issues.

Try this variation, where you capture the sheet name before adding each new workbook:
Code:
Sub ExpCsv()

Dim i As Integer
Dim myFileName As String

For i = 1 To 3

Sheets(i).Activate
myFileName = Sheets(i).Name
Range("A1:W645").Copy
Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs Filename:="S:\Com\Monthly\Test\" & myFileName & ".csv", FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True

Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,349
Messages
6,124,427
Members
449,158
Latest member
burk0007

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