Saving Multiple Sheets as Separate CSV files - VBA

jbellomy

New Member
Joined
May 18, 2006
Messages
11
I have a spreadsheet with 10 sheets. I need to set up some VBA code so the macro will save three of the tabs as separate .csv files for uploading into our software.

The three tabs are named: EID Upload, Wages with Locals Upload, Wages without Local Upload.

Right now, I would like the new file names to match the sheet names.

Thanks,

Jason
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Seems to work for me:
Code:
Sub asdf()
Dim ws As Worksheet

For Each ws In Sheets(Array("EID Upload", "Wages with Locals Upload", "Wages without Local Upload"))
   ws.SaveAs ws.Name, xlCSV
Next ws

End Sub
 
Upvote 0
Thanks Kristy. However, this appears to save the entire spreadsheet as a csv each time under different name.

I need my original spreadsheet to remain as a xls.

I was hoping to find a way where each csv saved will contain only the appropriate worksheet. It sounds like I will need to copy each worksheet to a new workbook, then save as csv.

Any suggestions?

Thanks,

Jason
 
Upvote 0
Hrm. Looks like you're right. I didn't notice it doing that before, I just saw there were 3 csv files created with only the data for the sheets.

You can copy them to a new workbook, then save that, though.

Code:
Sub asdf()
Dim ws As Worksheet, newWb As Workbook

Application.ScreenUpdating = False
For Each ws In Sheets(Array("EID Upload", "Wages with Locals Upload", "Wages without Local Upload"))
   ws.Copy
   Set newWb = ActiveWorkbook
   With newWb
      .SaveAs ws.Name, xlCSV
      .Close (False)
   End With
Next ws
Application.ScreenUpdating = True

End Sub
 
Upvote 0
After changing the above code to reflect the sheet names contained within my workbook the above code worked beautifully for me as well! I do have a couple of questions though...

I have a zip file that I download regularly that contains 20 or so individual workbooks containing only 1 worksheet per workbook (with unique sheet names). How difficult, or how would I, create additions to this code that would have it (from a new blank workbook):

Open each workbook contained in the folder, copy the sheet in that workbook to the new blank workbook and repeat this process until all the sheets from the individual workbooks are now saved as individual sheets in the new blank workbook with the name remaining the same. In short I would like to condense the 20 individual workbooks into one workbook with twenty sheets.

Any help would be appreciated.

Thanks,
J.



Code:
Sub ToCSV()


Dim ws As Worksheet, newWb As Workbook


Application.ScreenUpdating = False
For Each ws In Sheets(Array("List 1", "List 2"))
   ws.Copy
   Set newWb = ActiveWorkbook
   With newWb
      .SaveAs ws.Name, xlCSV
      .Close (False)
   End With
Next ws
Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,125
Members
453,021
Latest member
Justyna P

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