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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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