PDA

View Full Version : Split Workbook In To Sheets, Export To Folder and Move File



doug@powerstroke.us
05-22-2013, 12:12 AM
Hi all - I currently have a macro that exports all "Sheets" to separate CSV's based on the sheet name (See Below for script). I would like to add a step prior to create a folder based on two cells with a "-" between such as C2-B2. The current script will create the exports within the same folder that the .XLSM file is located. I would like the script to create the new folder within parent folder where the workbook is located and then export the CSVs into that newly created folder. Also, I would like the script at the end to copy a file called "header.txt" from the parent folder into the newly created folder.

Oh I forgot - Is there any way to prevent the export script from exporting the first sheet only?

Any assistance is appreciated!!

Current Export Macro---


Sub CSVExportTest2()

Dim tmpWS As Worksheet
Application.DisplayAlerts = False
For Each WS In ThisWorkbook.Worksheets

filePath = exportPath & "" & WS.Name & ".csv"

WS.Copy
Set tmpWS = ActiveSheet
tmpWS.SaveAs Filename:=filePath, FileFormat:=xlCSV
tmpWS.Parent.Close False
Next

End Sub

Excel Fox
05-22-2013, 11:50 AM
I'm sure you can modify this to suit your need.


Sub CSVExportTest2()

Dim tmpWS As Worksheet
Application.DisplayAlerts = False
Dim strPath As String, filePath As String
strPath = ThisWorkbook.Path & "\" & ThisWorkbook.Range("B2").Value & "-" & ThisWorkbook.Range("B2").Value
MkDir strPath
For Each ws In ThisWorkbook.Worksheets
If ws.Index > 1 Then
filePath = exportPath & "" & ws.Name & ".csv"
ws.Copy
Set tmpWS = ActiveSheet
tmpWS.SaveAs Filename:=filePath, FileFormat:=xlCSV
tmpWS.Parent.Close False
End If
Next
Name ThisWorkbook.Path & "\header.txt" As strPath & "\header.txt"


End Sub

doug@powerstroke.us
05-22-2013, 06:45 PM
Thanks for the code! I am although getting a compile error stating "Method or data member not found" at the ".Range" in the strPath line.