Macro to copy all worksheets into CSV files

cidfidou

Board Regular
Joined
Jan 19, 2009
Messages
163
Hi Excel Masters,

I am writing a new thread to make sure this is clear.

As i am useless in VBA, I am hoping that someone will write or copy an existing code to do the following :

- Save all worksheets of the current workbook into CSV files under the name of each worksheets
- the cherry on top of the cake would be a pop up window letting the user choose the location where to save the csv files (only once for all the worksheets)

As per usual, thanks in advance
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi,

Taking the previous solution from your earlier post, how about this?

Code:
Sub CreateWorkbooks()

Dim wbDest As Workbook
Dim wbSource As Workbook
Dim sht As Object
Dim strSavePath As String

On Error GoTo ErrorHandler

Application.ScreenUpdating = False

strSavePath = InputBox("Enter location to save files.", "File Destination", "C:\Temp\")

Set wbSource = ActiveWorkbook

For Each sht In wbSource.Sheets
sht.Copy
Set wbDest = ActiveWorkbook
wbDest.SaveAs strSavePath & sht.Name, xlCSV
wbDest.Close
Next

Application.ScreenUpdating = True

Exit Sub

ErrorHandler:
MsgBox "An error has occurred. Error number=" & Err.Number & ". Error description=" & Err.Description & "."
End Sub

Regards
 
Upvote 0
Hi,

Taking the previous solution from your earlier post, how about this?

Code:
Sub CreateWorkbooks()

Dim wbDest As Workbook
Dim wbSource As Workbook
Dim sht As Object
Dim strSavePath As String

On Error GoTo ErrorHandler

Application.ScreenUpdating = False

strSavePath = InputBox("Enter location to save files.", "File Destination", "C:\Temp\")

Set wbSource = ActiveWorkbook

For Each sht In wbSource.Sheets
sht.Copy
Set wbDest = ActiveWorkbook
wbDest.SaveAs strSavePath & sht.Name, xlCSV
wbDest.Close
Next

Application.ScreenUpdating = True

Exit Sub

ErrorHandler:
MsgBox "An error has occurred. Error number=" & Err.Number & ". Error description=" & Err.Description & "."
End Sub

Regards

Don't know why I never though of an input box :oops:
 
Upvote 0
Hi,

The input box does the trick, although it's a little rough & ready as the user could enter a location that doesn't exist. Only way around that would be to add some validation on what is entered, or going to lengths of creating a form that would allow you to detail drives etc.

But hopefully this will provide cidfidou with what he wants?

Regards
 
Upvote 0
Hi,

The input box does the trick, although it's a little rough & ready as the user could enter a location that doesn't exist. Only way around that would be to add some validation on what is entered, or going to lengths of creating a form that would allow you to detail drives etc.

But hopefully this will provide cidfidou with what he wants?

Regards

Yeah would require some work if there was going to be some other users on it, but as stands it's a nice easy solution to the problem, better than any of the ideas I had anyway :)
 
Upvote 0
As it can be incorporated easily, perhaps...
Code:
Sub x()

    Dim oFldr As FileDialog
    Dim strSavePath As String
    
    Set oFldr = Application.FileDialog(msoFileDialogFolderPicker)
    
    With oFldr

        .Title = "Select a directory"
        .AllowMultiSelect = False
        .InitialFileName = ActiveWorkbook.Path


        If .Show = True Then
            strSavePath = .SelectedItems(1)
        Else
            MsgBox "no folder selected...", vbExclamation
            Exit Sub
        End If
        
        MsgBox "Selected folder: " & strSavePath, vbInformation

    End With
    
End Sub
 
Last edited:
Upvote 0
Hi Cytop,

sry for being demanding, but could you please show me how to incorporate your above code in the one delivered by pascal?

Thanks in advance
 
Upvote 0
What about using Application.FileDialog to get a location?

e.g. (from VBA Excel Getting File Path (ends with folder) - Stack Overflow)

Code:
Sub SelectFolder()
    Dim diaFolder As FileDialog
    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show

    MsgBox diaFolder.SelectedItems(1)

    Set diaFolder = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,019
Members
448,938
Latest member
Aaliya13

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