Export Rng of cells for Selected sheets to CSV

becroft_g

New Member
Joined
Mar 2, 2014
Messages
3
Hi I have the below Code but would only like to export a range of some of my worksheets as CSV and also would not like it to change my sheet names on the original excel file.

Currently this exports all the sheets and renames each sheet to include the DDMMYYYY

Public Sub SaveWorksheetsAsCsv()
Dim wb As Workbook
Dim WS As Worksheet
Dim SaveToDirectory As String
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat

SaveToDirectory = "C:\Users\Greg.Becroft\Desktop\P\"
For Each WS In ThisWorkbook.Worksheets
WS.SaveAs SaveToDirectory & WS.Name & Format(Now(), "DDMMYYYY"), xlCSV
Next
Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello Becroft_g,
I have made some changes to your code.
Try now, I hope so that you get what you want.
VBA Code:
Public Sub SaveWorksheetsAsCsv()

    Dim wn As Window
    Dim WS As Worksheet
    Dim SaveToDirectory As String
    Dim CurrentWorkbook As String
    Dim CurrentFormat As Long
    
    Application.ScreenUpdating = False
    CurrentWorkbook = ThisWorkbook.FullName
    CurrentFormat = ThisWorkbook.FileFormat
    SaveToDirectory = "C:\Users\Greg.Becroft\Desktop\P\"
    Workbooks.Add
    Set wn = ActiveWindow
    For Each WS In ThisWorkbook.Worksheets
        WS.UsedRange.Copy
        wn.ActiveSheet.[a1].PasteSpecial
        ActiveWorkbook.SaveAs SaveToDirectory & WS.Name & "_" & Format(Now(), "DDMMYYYY"), xlCSV
    Next
    Application.DisplayAlerts = False
    wn.Close
    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
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