Created
September 15, 2019 12:58
-
-
Save lejarx/555b2a1c81fca96021afa03bab5d96c4 to your computer and use it in GitHub Desktop.
VBA: Batch convert Excel files to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub WorkbooksSaveAsCsvToFolder() | |
'UpdatebyExtendoffice20181031 | |
Dim xObjWB As Workbook | |
Dim xObjWS As Worksheet | |
Dim xStrEFPath As String | |
Dim xStrEFFile As String | |
Dim xObjFD As FileDialog | |
Dim xObjSFD As FileDialog | |
Dim xStrSPath As String | |
Dim xStrCSVFName As String | |
Application.ScreenUpdating = False | |
Application.EnableEvents = False | |
Application.Calculation = xlCalculationManual | |
On Error Resume Next | |
Set xObjFD = Application.FileDialog(msoFileDialogFolderPicker) | |
xObjFD.AllowMultiSelect = False | |
xObjFD.Title = "Kutools for Excel - Select a folder which contains Excel files" | |
If xObjFD.Show <> -1 Then Exit Sub | |
xStrEFPath = xObjFD.SelectedItems(1) & "\" | |
Set xObjSFD = Application.FileDialog(msoFileDialogFolderPicker) | |
xObjSFD.AllowMultiSelect = False | |
xObjSFD.Title = "Kutools for Excel - Select a folder to locate CSV files" | |
If xObjSFD.Show <> -1 Then Exit Sub | |
xStrSPath = xObjSFD.SelectedItems(1) & "\" | |
xStrEFFile = Dir(xStrEFPath & "*.xls*") | |
Do While xStrEFFile <> "" | |
Set xObjWB = Workbooks.Open(Filename:=xStrEFPath & xStrEFFile) | |
xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".") - 1) & ".csv" | |
xObjWB.SaveAs Filename:=xStrCSVFName, FileFormat:=xlCSV | |
xObjWB.Close savechanges:=False | |
xStrEFFile = Dir | |
Loop | |
Application.Calculation = xlCalculationAutomatic | |
Application.EnableEvents = True | |
Application.ScreenUpdating = True | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From https://www.extendoffice.com/documents/excel/5537-excel-batch-convert-to-csv.html