Last active
February 7, 2020 09:59
-
-
Save overing/d578fe4c70f1713d2980c5fc0c48404a to your computer and use it in GitHub Desktop.
Output xls file sheets to csv (excel vba/bat call vbs)
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
Set FSO = CreateObject("Scripting.FileSystemObject") | |
Sub 掃描工作目錄檔案() '表單上面放按鈕或是用 [Alt]+[F8] 啟動巨集 | |
inputFolderPath = PickFolder(ThisWorkbook.Path, "Select folder for search xls/xlsx/xlm") | |
If inputFolderPath = "" Then Exit Sub | |
outputFolderPath = PickFolder(inputFolderPath, "Select folder for output sheets as csv") | |
If outputFolderPath = "" Then Exit Sub | |
Execute inputFolderPath, outputFolderPath | |
End Sub | |
Sub Execute(inputFolderPath, outputFolderPath) | |
Set workbookFolder = FSO.GetFolder(inputFolderPath) | |
selfPath = ThisWorkbook.Path | |
selfName = ThisWorkbook.Name | |
Sheet1.Cells.Clear | |
For Each file In workbookFolder.Files | |
filename = file.Name | |
ext = GetExtension(filename) | |
If file.Path <> selfPath And filename <> selfName _ | |
And Left(filename, 2) <> "~$" _ | |
And (ext = "xls" Or ext = "xlsx" Or ext = "xlsm") Then | |
i = i + 1 | |
Sheet1.Cells(i, 1).Value = file.Path | |
SaveEachSheetAsCsv file.Path, outputFolderPath | |
End If | |
Next file | |
End Sub | |
Function PickFolder(default, title) | |
Set picker = Application.FileDialog(msoFileDialogFolderPicker) | |
With picker | |
.title = title | |
.AllowMultiSelect = False | |
.InitialFileName = default | |
If .Show <> -1 Then | |
PcikFolder = "" | |
Else | |
PickFolder = .SelectedItems(1) | |
End If | |
End With | |
End Function | |
Function GetExtension(filename) | |
GetExtension = Right(filename, Len(filename) - InStrRev(filename, ".")) | |
End Function | |
Sub SaveEachSheetAsCsv(filePath, outputFolderPath) | |
Set extraBook = Application.Workbooks.Open(filePath) | |
For Each extraSheet In extraBook.Sheets | |
sheetName = extraSheet.Name | |
outputName = outputFolderPath & "\" & extraBook.Name & "-" & sheetName & ".csv" | |
If FSO.FileExists(outputName) Then FSO.DeleteFile outputName | |
extraSheet.Copy | |
extraSheet.SaveAs outputName, XlFileFormat.xlCSV | |
Application.ActiveWorkbook.Close False | |
Next extraSheet | |
extraBook.Close False | |
End Sub |
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
@ECHO off | |
SETLOCAL EnableDelayedExpansion | |
SET VBS_FILE=%~0.vbs | |
IF EXIST "%VBS_FILE%" DEL /F /Q "%VBS_FILE%" | |
FOR /F "delims=" %%L IN (%~0) DO ^ | |
IF "!OUTPUT!"=="1" (ECHO.%%L>>"%VBS_FILE%") ELSE IF "%%L"==":BEGIN_VBS" SET OUTPUT=1 | |
cscript "%VBS_FILE%" && DEL "%VBS_FILE%" | |
GOTO :EOF | |
:BEGIN_VBS | |
Set FSO = CreateObject("Scripting.FileSystemObject") | |
Set Application = CreateObject("Excel.Application") | |
Sub Main() | |
inputFolderPath = FSO.GetParentFolderName(WScript.ScriptFullName) | |
inputFolderPath = PickFolder(inputFolderPath, "Select folder for search xls/xlsx/xlm") | |
If inputFolderPath = "" Then | |
Wscript.Echo "(input folder not selected, going to quit)" | |
Wscript.Quit | |
End If | |
outputFolderPath = PickFolder(inputFolderPath, "Select folder for output sheets as csv") | |
If outputFolderPath = "" Then | |
Wscript.Echo "(output folder not selected, going to quit)" | |
Wscript.Quit | |
End If | |
Execute inputFolderPath, outputFolderPath | |
End Sub | |
Sub Execute(inputFolderPath, outputFolderPath) | |
Set workbookFolder = FSO.GetFolder(inputFolderPath) | |
For Each file In workbookFolder.Files | |
filename = file.Name | |
ext = GetExtension(filename) | |
If file.Path <> selfPath And filename <> selfName _ | |
And Left(filename, 2) <> "~$" _ | |
And (ext = "xls" Or ext = "xlsx" Or ext = "xlsm") Then | |
i = i + 1 | |
Wscript.Echo file.Path | |
SaveEachSheetAsCsv file.Path, outputFolderPath | |
End If | |
Next | |
End Sub | |
Function PickFolder(default, title) | |
Set picker = Application.FileDialog(4) '4: select folder | |
With picker | |
.title = title | |
.AllowMultiSelect = False | |
.InitialFileName = default | |
If .Show <> -1 Then | |
PcikFolder = "" | |
Else | |
PickFolder = .SelectedItems(1) | |
End If | |
End With | |
End Function | |
Function GetExtension(filename) | |
GetExtension = Right(filename, Len(filename) - InStrRev(filename, ".")) | |
End Function | |
Sub SaveEachSheetAsCsv(filePath, outputFolderPath) | |
Set extraBook = Application.Workbooks.Open(filePath) | |
For Each extraSheet In extraBook.Sheets | |
sheetName = extraSheet.Name | |
outputName = outputFolderPath & "\" & extraBook.Name & "-" & sheetName & ".csv" | |
If FSO.FileExists(outputName) Then FSO.DeleteFile outputName | |
extraSheet.Copy | |
extraSheet.SaveAs outputName, 6 '6: csv formet | |
Application.ActiveWorkbook.Close False | |
Next | |
extraBook.Close False | |
End Sub | |
Main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment