我正在尝试将文本文件转换为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
答案 0 :(得分:3)
我使用正则表达式将数据转换为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文件并将其另存为工作簿。
答案 1 :(得分:2)
我会抛弃另一种解决方案。只需使用Excel的TextToColumns()
功能即可。告诉它使用Space
(第8个参数= True
)作为分隔符并使用Treat consecutive delimiters as one
(第4个参数= 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
。
答案 2 :(得分:1)
从评论中,我认为跳过VBA可能更容易,将.txt文件扩展名更改为.csv。然后,当您在Excel中打开它时,您将获得包含所有数据的列。
在顶部的“数据”标签下,您会看到“文字到列”。突出显示A列,选择“Text to Columns”,然后选择“Delimited”,然后点击“Next”,然后选择“Space”。您将看到如何在其下方拆分数据的预览。如果看起来不错,可以单击“完成”以使用新的拆分数据覆盖列表A,或单击“下一步”以选择要启动的特定单元格。
这应该让你相当远,而且可能不完美,所以请在此之后告诉我它的样子(或者如果你有任何问题)。
答案 3 :(得分:1)
更快的方法:将文件转换为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