Exporting multiple worksheets as CSV

J

Joe Sheehan

I'm looking for a way to automatically export multiple worksheets as
CSV. I have some code that will export the Active Sheet. I'm not
looking for fancy renaming - I basically just need to turn each
worksheet in an XLS file into its own CSV file. Has anyone already
done this?

Thanks
Joe
 
D

Dave Peterson

I bet lots of people have a version that did what you asked. Here's one:

Option Explicit
Sub testme()

Dim newWks As Worksheet
Dim wks As Worksheet

For Each wks In ActiveWorkbook.Worksheets
wks.Copy 'to a new workbook
Set newWks = ActiveSheet
With newWks
.SaveAs Filename:="C:\temp\" & wks.Name, FileFormat:=xlCSV
.Parent.Close savechanges:=False
End With
Next wks

MsgBox "done with: " & ActiveWorkbook.Name

End Sub
 
M

Mike O

I'm a complete newbie at this stuff...how would you edit this script so
it only exports a "target" sheet as a CSV?

So instead of multiples, it only exports one.

Thanks,

Mike
 
M

Mike O

I'm a newbie at this...how would you change that script to make it work
if you wanted to "target" the sheet you wanted to export as CSV?

Basically, instead of multiple sheets, just the sheet you want.

Thanks!

Mike
 
D

Dave Peterson

How about:

Option Explicit
Sub testme()

Dim newWks As Worksheet
Dim wks As Worksheet

set wks = activeworkbook.worksheets("mysheetnamehere")

wks.Copy 'to a new workbook
Set newWks = ActiveSheet
With newWks
.SaveAs Filename:="C:\temp\" & wks.Name, FileFormat:=xlCSV
.Parent.Close savechanges:=False
End With


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top