Excel VBA:将行拆分为多行

时间:2017-04-20 17:45:30

标签: excel vba excel-vba

我有一行单元格,其中包含需要转换为多行的数据。

  

18-Apr-17 11:00:30 SkyFlyer1_Leg1 319437.222 146279.951 -32.768 SkyFlyer1_Leg2 319417.07 146268.105 -32.768 SkyFlyer1_Leg3 319410.548 146268.368 -32.768

这是一条线的一半(有8条腿)。空格表示单元格之间的区分。

每个" SkyFlyer_Leg *"需要一个carrage返回来把它带到一个新的线上。

这些行正在使用另一个从CSV解析它们的脚本进入工作表。

Option Explicit

Sub CSVParser_99()

Dim i As Long
Dim x As Long
Dim LastRow As Long
Dim PasteRow As Long

   With Sheets("CSV Paste")
        LastRow = .Range("A3").End(xlDown).Row
        For i = 3 To LastRow
            PasteRow = Sheets("Working Sheet 1").Cells(Sheets("Working Sheet 1").Rows.Count, "A").End(xlUp).Row
        .Range(.Range("A" & i), .Range("A" & i).End(xlToRight)).Copy Destination:=Sheets("Working Sheet 1").Range("A" & PasteRow + 1)

        Call RowDiv

    Next i
End With

End Sub
Sub RowDiv()

Dim Row1 As Variant

With Sheets("Working Sheet 1")
    .Range("C6000").End(xlUp).Select
End With

With Row1.ActiveCell

End With

End Sub

这是我到目前为止所做的,包括将代码带入此页面的脚本。 (感谢Shai Rado到目前为止的帮助)

我想要的结果将数据排列如下:

  

18-APR-17 | 11时零零分30秒

     

SkyFlyer1_Leg1 | 319437.222 | 146279.951 | -32.768

     

SkyFlyer1_Leg2 | 319417.070 | 146268.105 | -32.768

     

SkyFlyer1_Leg3 | 319410.548 | 146268.368 | -32.768

非常感谢任何帮助。

干杯,J

1 个答案:

答案 0 :(得分:0)

也许这会起作用

Sub CSVParser_99()

    Dim i As Long
    Dim x As Long
    Dim LastRow As Long
    Dim PasteRow As Long

       With Sheets("CSV Paste")
            LastRow = .Range("A3").End(xlDown).Row
            For i = 3 To LastRow
                PasteRow = Sheets("Working Sheet 1").Cells(Sheets("Working Sheet 1").Rows.Count, "A").End(xlUp).Row
            .Range(.Range("A" & i), .Range("A" & i).End(xlToRight)).Copy Destination:=Sheets("Working Sheet 1").Range("A" & PasteRow + 1)

            Call RowDiv

        Next i
    End With

End Sub


Sub RowDiv()

    Dim Row1 As Range

    With Sheets("Working Sheet 1")
        Set Row1 = .Range("A6000").End(xlUp)
    End With

    With Row1

        .TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
                TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
            Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
            :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
            Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
            ), Array(14, 1)), TrailingMinusNumbers:=True
    End With

End Sub