Hi,
I currently have a macro that runs on all Excel files in a folder and exports all the sheets within those files out as a csv.
I'd like to know if it's possible run the macro so that these csv files that are created are saved with sheet name and prefix : first 6 letters of the original Excel file name.
For example:
File 1 name : 123456 ABC ABC and sheets are called XYZ, GHB, TBA
File 2 name: 456789 ABC ABC and sheets are called XYZ, GHB, TBA
Instead of output being same copies of XYZ, GHB, TBA csv file names, I'd like them to be as "123456 XYZ.csv", "123456 GHB.csv", "456789 XYZ.csv", etc.
Below is the code that exports sheet to csv:
Sub ExportSheetsToCSV()
Dim xWs As Worksheet
Dim xcsvFile As String
ChDrive "C"
ChDir "C:\Users\_____"
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.Copy
xcsvFile = CurDir & "\" & xWs.Name & ".csv"
Application.ActiveWorkbook.SaveAs fileName:=xcsvFile, _
FileFormat:=xlCSV, CreateBackup:=False
Application.ActiveWorkbook.Saved = True
Application.ActiveWorkbook.Close
Next
End Sub
I currently have a macro that runs on all Excel files in a folder and exports all the sheets within those files out as a csv.
I'd like to know if it's possible run the macro so that these csv files that are created are saved with sheet name and prefix : first 6 letters of the original Excel file name.
For example:
File 1 name : 123456 ABC ABC and sheets are called XYZ, GHB, TBA
File 2 name: 456789 ABC ABC and sheets are called XYZ, GHB, TBA
Instead of output being same copies of XYZ, GHB, TBA csv file names, I'd like them to be as "123456 XYZ.csv", "123456 GHB.csv", "456789 XYZ.csv", etc.
Below is the code that exports sheet to csv:
Sub ExportSheetsToCSV()
Dim xWs As Worksheet
Dim xcsvFile As String
ChDrive "C"
ChDir "C:\Users\_____"
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.Copy
xcsvFile = CurDir & "\" & xWs.Name & ".csv"
Application.ActiveWorkbook.SaveAs fileName:=xcsvFile, _
FileFormat:=xlCSV, CreateBackup:=False
Application.ActiveWorkbook.Saved = True
Application.ActiveWorkbook.Close
Next
End Sub