save a single sheet in a workbook as a separate csv file

bmcclure

New Member
Joined
Sep 19, 2013
Messages
24
hi,

i want to save a single worksheet named TransferFile in a workbook as a separate CSV file. This is as far as i've gotten, but it saves every sheet as a new CSV file instead of just the one i want. not sure how to get it to do what i want.

Any help is greatly appreciated!
-B

Sub ExportFileUploadAsCSV()

Dim ws As Worksheet
Dim sFolderPath As String

sFolderPath = "B:\Operations\Brian\MACRO PROJECTS\Bond Upload\"

For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then
ws.Copy

ActiveWorkbook.SaveAs _
Filename:=sFolderPath & ws.Name, _
FileFormat:=xlCSV

ActiveWorkbook.Saved = True
ActiveWorkbook.Close False
End If

Next ws


End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
That is because you are looping through every sheet in the workbook. Get rid of that loop, and try something like this:
VBA Code:
Sub ExportFileUploadAsCSV()

    Dim ws As Worksheet
    Dim sFolderPath As String

    sFolderPath = "B:\Operations\Brian\MACRO PROJECTS\Bond Upload\"

    Set ws = Sheets("TransferFile")

    ws.Activate
    ActiveWorkbook.SaveAs _
        Filename:=sFolderPath & ws.Name, _
        FileFormat:=xlCSV

    ActiveWorkbook.Saved = True
    ActiveWorkbook.Close False

End Sub
 
Upvote 0
this works thank you. one more question. is there a way to do it where it doesnt close the original file and open a blank workbook?

That is because you are looping through every sheet in the workbook. Get rid of that loop, and try something like this:
VBA Code:
Sub ExportFileUploadAsCSV()

    Dim ws As Worksheet
    Dim sFolderPath As String

    sFolderPath = "B:\Operations\Brian\MACRO PROJECTS\Bond Upload\"

    Set ws = Sheets("TransferFile")

    ws.Activate
    ActiveWorkbook.SaveAs _
        Filename:=sFolderPath & ws.Name, _
        FileFormat:=xlCSV

    ActiveWorkbook.Saved = True
    ActiveWorkbook.Close False

End Sub
 
Upvote 0
Just get rid of the last two lines before the "End Sub"; these two in particular:
VBA Code:
   ActiveWorkbook.Saved = True
    ActiveWorkbook.Close False
(the "ActiveWorkbook.Close..." is a dead giveaway!).

Note however, that the file left open will be the CSV file. So, if you make any changes and want to save it to the Excel file, you will have to do a SaveAs and select/enter the original file name and format.
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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