Excel VBS-将制表符分隔的CSV文件转换为XLSX
我之前已经发布了一个类似的问题,将CSV文件转换为xlsx 这个问题可以在这里找到: 代码:VBS 问题是,上述内容在我需要转换为xlsx的当前CSV文件上不起作用 从我所看到的,上面的代码以逗号分隔的形式读取CSV文件,但我当前使用的文件是以制表符分隔的 下面是我在excel中记录的一个宏,我对文件进行了数据导入,虽然我可以在excel中重复使用此代码进行转换,但我更愿意在此之前在脚本中进行转换 下面是代码输出和预期输出的屏幕截图: 第1行:电流输出 第2行:预期产出 代码:VBA 答案如下: 这适用于“分隔”csv/文本文件的大多数实例Excel VBS-将制表符分隔的CSV文件转换为XLSX,excel,csv,vbscript,Excel,Csv,Vbscript,我之前已经发布了一个类似的问题,将CSV文件转换为xlsx 这个问题可以在这里找到: 代码:VBS 问题是,上述内容在我需要转换为xlsx的当前CSV文件上不起作用 从我所看到的,上面的代码以逗号分隔的形式读取CSV文件,但我当前使用的文件是以制表符分隔的 下面是我在excel中记录的一个宏,我对文件进行了数据导入,虽然我可以在excel中重复使用此代码进行转换,但我更愿意在此之前在脚本中进行转换 下面是代码输出和预期输出的屏幕截图: 第1行:电流输出 第2行:预期产出 代码:VBA 答案如
我所做的只是将
.TextFileOtherDelimiter=“#”
更改为.TextFileOtherDelimiter=False
,将.TextFileTabDelimiter=False
更改为。那么,您的实际问题是什么?如何将您的VBA转换为VBS?@pnuts请看我找到了合适的解决方案并发布在下面。@luklag请看我找到了合适的解决方案并发布在下面。
'Constants
Const xlOpenXMLWorkbook = 51 '(without macro's in 2007-2016, xlsx)
Const xlOpenXMLWorkbookMacroEnabled = 52 '(with or without macro's in 2007-2016, xlsm)
Const xlExcel12 = 50 '(Excel Binary Workbook in 2007-2016 with or without macro's, xlsb)
Const xlExcel8 =56 '(97-2003 format in Excel 2007-2016, xls)
' Extensions for old and new files
strExcel = "xlsx"
strCSV = "csv"
strXLS = "xls"
' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "B:\EE\EE29088597\Files"
' Access the folder to process
Set objFolder = objFSO.GetFolder(strFolder)
' Load Excel (hidden) for conversions
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
' Process all files
For Each objFile In objFolder.Files
' Get full path to file
strPath = objFile.Path
' Only convert CSV files
If LCase(objFSO.GetExtensionName(strPath)) = LCase(strCSV) Or LCase(objFSO.GetExtensionName(strPath)) = LCase(strXLS) Then
' Display to console each file being converted
Wscript.Echo "Converting """ & strPath & """"
' Load CSV into Excel and save as native Excel file
Set objWorkbook = objExcel.Workbooks.Open(strPath, False, True)
strNewPath = objFSO.GetParentFolderName(strPath) & "\" & objFSO.GetBaseName(strPath) & "." & strExcel
objWorkbook.SaveAs strNewPath, xlOpenXMLWorkbook
objWorkbook.Close False
Set objWorkbook = Nothing
End If
Next
'Wrap up
objExcel.Quit
Set objExcel = Nothing
Set objFSO = Nothing
Sub Import_CSV()
'
' Import_CSV Macro
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;Location\test.csv", Destination _
:=Range("$A$1"))
.CommandType = 0
.Name = "test"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Const xlTextQualifierDoubleQuote = 1
Const xlInsertDeleteCells = 1
Const xlDelimited = 1
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts= False
strCSVfile = "\input.csv"
strXLSfile = "\input.xlsx"
Set objWorkbook = objExcel.Workbooks.Add
Set objWorkSheet = objWorkbook.Worksheets(1)
With objWorkSheet.QueryTables.Add("TEXT;" & strCSVfile, objWorkSheet.Range("$A$1"))
.Name = "input"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "#"
.TextFileColumnDataTypes = Array(1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh False
End With
'Save Spreadsheet, 51 = Excel 2007-2010
objWorkSheet.SaveAs strXLSfile, 51
'Release Lock on Spreadsheet
objExcel.Quit()
Set ObjExcel = Nothing