reuben_rambo
New Member
- Joined
- Mar 13, 2023
- Messages
- 6
- Office Version
- 365
- 2021
- 2019
- Platform
- Windows
I have a current VBA code that works and is written below.
To run the VBA code, you must open the XLSM file and then run the VBA code, where it then saves the worksheets as CSV files into a particular folder with the CSV file names are associated with the exact worksheet names.
Question 1
Can the below VBA code be improved, where the names of the CSV files include the workbook name and worksheet name?
The EXAMPLE
I have a ONE XLSM file named:
test1.xlsm
The XLSM file has 2 worksheets named:
1_WSUS1
1_WSUS2
The below VBA code will convert the sheets to CSV files, which I want. The CSV files will be named:
1_WSUS1.csv
1_WSUS2.csv
My specific question related to the example is:
Can the below VBA code be improved, where the names of the CSV files include the workbook name (i.e., test1) .... so, the names of the newly created CSV files are:
test1_1_WSUS1.csv
test1_1_WSUS2.csv
Any help here would be greatly appreciated.
The VBA code
Public Sub SaveWorksheetsAsCsv()
Dim xWs As Worksheet
Dim xDir As String
Dim folder As FileDialog
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.SaveAs xDir & "\" & xWs.Name, xlCSV
Next
End Sub
Thanks,
Reuben
To run the VBA code, you must open the XLSM file and then run the VBA code, where it then saves the worksheets as CSV files into a particular folder with the CSV file names are associated with the exact worksheet names.
Question 1
Can the below VBA code be improved, where the names of the CSV files include the workbook name and worksheet name?
The EXAMPLE
I have a ONE XLSM file named:
test1.xlsm
The XLSM file has 2 worksheets named:
1_WSUS1
1_WSUS2
The below VBA code will convert the sheets to CSV files, which I want. The CSV files will be named:
1_WSUS1.csv
1_WSUS2.csv
My specific question related to the example is:
Can the below VBA code be improved, where the names of the CSV files include the workbook name (i.e., test1) .... so, the names of the newly created CSV files are:
test1_1_WSUS1.csv
test1_1_WSUS2.csv
Any help here would be greatly appreciated.
The VBA code
Public Sub SaveWorksheetsAsCsv()
Dim xWs As Worksheet
Dim xDir As String
Dim folder As FileDialog
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.SaveAs xDir & "\" & xWs.Name, xlCSV
Next
End Sub
Thanks,
Reuben