我必须将文本文件导入excel。这些文本文件是巨大的,数据是行,我必须转置到列,我有一个简单的代码,如下所示。
Sub extraction()
Set tr = Selection
j = 2
For i = 1 To tr.Rows.Count
If Left(tr.Cells(i, 1), 6) = "(DATE)" Then
j = j + 1
Cells(j, 5) = Mid(tr.Cells(i, 1), 7, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 20) = "(SEA/WIND DIRECTION)" Then
Cells(j, 6) = Mid(tr.Cells(i, 1), 21, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 16) = "(SEA/WIND POWER)" Then
Cells(j, 7) = Mid(tr.Cells(i, 1), 17, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 7) = "(SPEED)" Then
Cells(j, 8) = Mid(tr.Cells(i, 1), 8, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 7) = "(MILES)" Then
Cells(j, 9) = Mid(tr.Cells(i, 1), 8, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 10) = "(FUEL AUX)" Then
Cells(j, 10) = Mid(tr.Cells(i, 1), 11, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 21) = "(TOTAL STEAMING TIME)" Then
Cells(j, 11) = Mid(tr.Cells(i, 1), 22, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 5) = "(RPM)" Then
Cells(j, 12) = Mid(tr.Cells(i, 1), 6, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 6) = "(SLIP)" Then
Cells(j, 13) = Mid(tr.Cells(i, 1), 7, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 7) = "(POWER)" Then
Cells(j, 14) = Mid(tr.Cells(i, 1), 8, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 14) = "(DISPLACEMENT)" Then
Cells(j, 15) = Mid(tr.Cells(i, 1), 15, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 13) = "(FUEL M/E HS)" Then
Cells(j, 16) = Mid(tr.Cells(i, 1), 14, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 13) = "(FUEL M/E LS)" Then
Cells(j, 17) = Mid(tr.Cells(i, 1), 14, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 9) = "(OIL CYL)" Then
Cells(j, 18) = Mid(tr.Cells(i, 1), 10, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 15) = "(STEAMING TIME)" Then
Cells(j, 19) = Mid(tr.Cells(i, 1), 16, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 22) = "(STEAMING TIME M/E HS)" Then
Cells(j, 20) = Mid(tr.Cells(i, 1), 23, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 22) = "(STEAMING TIME M/E LS)" Then
Cells(j, 21) = Mid(tr.Cells(i, 1), 23, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 10) = "(FUEL M/E)" Then
Cells(j, 22) = Mid(tr.Cells(i, 1), 11, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 11) = "(ECO SPEED)" Then
Cells(j, 23) = Mid(tr.Cells(i, 1), 12, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 11) = "(MILES ECO)" Then
Cells(j, 24) = Mid(tr.Cells(i, 1), 12, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 8) = "(BHP KW)" Then
Cells(j, 25) = Mid(tr.Cells(i, 1), 9, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 10) = "(SEA/WIND)" Then
Cells(j, 6) = Mid(tr.Cells(i, 1), 11, Len(tr.Cells(i, 1)))
ElseIf Left(tr.Cells(i, 1), 16) = "(SEA WIND POWER)" Then
Cells(j, 7) = Mid(tr.Cells(i, 1), 17, Len(tr.Cells(i, 1)))
End If
Next i
End Sub
现在,问题是,在SEA/WIND POWER
列中,有像3/2这样的数据,当自动导入时会转换为日期格式(02-Mar)。
我想要的是原始文本(例如3/2)或更大的数字。作为VBA的新手,我无法解决问题。
任何帮助将不胜感激。谢谢:))
答案 0 :(得分:1)
在导入txt文件之前在Sub下面运行
Sub Prep()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
ws.Cells.NumberFormat = "@"
Next
End Sub
此子句遍历所有工作表并将所有单元格格式化为文本,因此导入的值将以原始格式显示。
答案 1 :(得分:0)
获得此功能的另一种肮脏方法是向其添加撇号。例如:
<!-- language: VB -->
Cells(j, 5) = "'" & Mid(tr.Cells(i, 1), 7, Len(tr.Cells(i, 1)))
这将导入'3/2