0

我必须将文本文件导入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的新手,我无法解决这个问题。

任何帮助将不胜感激。谢谢 :)

4

2 回答 2

1

在导入txt文件之前在Sub下面运行

Sub Prep()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
        ws.Cells.NumberFormat = "@"
    Next
End Sub

这个子遍历所有工作表并将所有单元格格式化为文本,因此导入的值将以原始格式显示。

于 2013-08-13T11:40:20.120 回答
0

另一个肮脏的方法是给它添加一个撇号。例如:

<!-- language: VB -->
    Cells(j, 5) = "'" & Mid(tr.Cells(i, 1), 7, Len(tr.Cells(i, 1)))

这将导入'3/2

于 2014-02-17T13:29:55.287 回答