[英]Convert txt file to excel in vbscript
我正在嘗試將文本文件轉換為Excel工作表。 這就是格式的樣子。
我曾嘗試編寫腳本,但目前所做的只是覆蓋我當前添加列標題的文本文件。 它不會添加我的文本文件中的任何數據。 任何人都可以幫助我理解我做錯了什么。
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
strInput=InputBox("Enter name of File in C:\Users\spencerr\Desktop\MyProject\bin\")
'ask user for file name
Set wb = objExcel.Workbooks.Open("C:\Users\bob\Desktop\MyProject\bin\" & strInput)
'Delete labels in log
For i = 1 To 5
Set objRange = objExcel.Cells(1, i).EntireColumn
objRange.Delete
Next
Set activeCell = objExcel.Cells(1, 2)
Dim intVal
Dim comVal
Dim primeRow
Dim largestRow
Dim largestDec
Dim row
primeRow = 0
'filter out one measurement per second
Do Until IsEmpty(activeCell)
primeRow = primeRow + 1
'get base integer of first value by chopping off decimal
intVal = Fix(activeCell.Value)
comVal = intVal
'get all consecutive rows that have same base integer
Do While intVal = comVal
row = activeCell.Row
Set activeCell = objExcel.Cells((row + 1), 2)
comVal = Fix(activeCell.Value)
Loop
'highest row number that contains the base integer
largestRow = row
'delete all the rows up to the largest row
j = primeRow
Do While j < largestRow
Set deleteRow = objExcel.Cells(primeRow, 2).EntireRow
deleteRow.Delete
j = j + 1
Loop
'compare the value right below the exact second and the value right above to see
'which is closer to the exact second
Set activeCell = objExcel.Cells(primeRow, 2)
largestDec = activeCell.Value
Set activeCell = objExcel.Cells((primeRow + 1), 2)
comVal = activeCell.Value
if (((intVal + 1) - largestDec) > (comVal - (intVal + 1))) Then
objExcel.Cells(primeRow, 2).EntireRow.Delete
End If
Loop
'round all the seconds that are left to the nearesr second
Set activeCell = objExcel.Cells(1, 2)
Do Until IsEmpty(ActiveCell)
row = activeCell.row
objExcel.Cells(row, 2) = Round(activeCell.Value)
Set activeCell = objExcel.Cells(row + 1, 2)
Loop
'add labels for KML conversion
objExcel.Cells(1,1).EntireRow.Insert
objExcel.Cells(1, 2).Value = "Description"
objExcel.Cells(1, 3).Value = "Latitude"
objExcel.Cells(1, 4). Value = "Longitude"
wb.Save
wb.Close
objExcel.Quit
我使用正則表達式將數據轉換為CSV格式:
Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile = fso.OpenTextFile("C:\path\to\input.txt")
Set outFile = fso.OpenTextFile("C:\path\to\output.csv", 2, True)
Set re = New RegExp
re.Pattern = "^week: (\d+) seconds: (\d+\.\d+) x: (\d+\.\d+) " & _
"y: (-\d+\.\d+) heading: (\d+)$"
re.IgnoreCase = True
outFile.WriteLine "Week,Seconds,X,Y,Heading"
Do Until inFile.AtEndOfStream
For Each m In re.Execute(inFile.ReadLine)
outFile.WriteLine m.Submatches(0) & "," & m.Submatches(1) & "," & _
m.Submatches(2) & "," & m.Submatches(3) & "," & m.Submatches(4)
Next
Loop
inFile.Close
outFile.Close
然后,您可以使用Excel打開CSV文件並將其另存為工作簿。
我會拋出另一個解決方案。 只需使用Excel的TextToColumns()
函數即可。 告訴它使用Space
(第8個參數= True
)作為分隔符並將Treat consecutive delimiters as one
(第四個參數= True
)。
Const xlDelimited = 1
Const xlDoubleQuote = 1
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Open "c:\path\to\text.txt"
With objExcel.ActiveSheet
.Columns("A:A").TextToColumns .Range("A1"), xlDelimited, xlDoubleQuote, True, , , , True
End With
或者,長形式:
With objExcel.ActiveSheet
.Columns("A:A").TextToColumns _
.Range("A1"), _ ' Destination
xlDelimited, _ ' Data Type
xlDoubleQuote, _ ' Text Qualifier
True, _ ' Consecutive Delimiters?
, _ ' Use Tab for Delimiter?
, _ ' Use Semicolon for Delimiter?
, _ ' Use Comma for Delimiter?
True ' Use Space for Delimiter?
End With
這將使您的數據進入正確的列。 然后只需刪除“標簽”列:
.Range("A:A,C:C,E:E,G:G,I:I").Delete
並將其另存為CSV
。
從評論中,我認為跳過VBA可能更容易,將.txt文件擴展名更改為.csv。 然后,當您在Excel中打開它時,您將獲得包含所有數據的列。
在頂部,在“數據”選項卡下,您將看到“文本到列”。 突出顯示A列,選擇“Text to Columns”,然后選擇“Delimited”,然后點擊“Next”,然后選擇“Space”。 您將看到如何在其下方拆分數據的預覽。 如果看起來不錯,可以單擊“完成”以使用新的拆分數據覆蓋列表A,或單擊“下一步”以選擇要啟動的特定單元格。
這應該會讓你相當遠,它可能不完美,所以讓我知道它之后的樣子(或者如果你有任何問題)。
更快的方法:將文件轉換為csv。 由於您的源是固定寬度,因此最簡單的方法是將您需要的位復制到新行。
sep = ";" 'or , (depends on your language settings)'
header = "week" & sep & "seconds" & sep
line = "WEEK: 1799 SECONDS: 251731.358 X:32.896391 Y:-117.200281 Heading: 178"
csvLine = mid(line,7,4) & sep & mid(line,22,10) & sep 'etc..'
'write to your csv file, here I only echo to the screen
Wscript.echo header
Wscript.echo csvLine
'week;seconds;
'1799;251731.358;
使用此方法更快,您不需要在PC上安裝Excel
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.