简体   繁体   English

Excel VBA:将一行拆分为多行

[英]Excel VBA: Splitting a row into multiple rows

I have a row of cells that contains data that needs to be turned into multiple rows. 我有一排单元格,其中包含需要转换为多行的数据。

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 17年4月18日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

This is half of one line (there are 8 legs). 这是一条线的一半(有8条腿)。 The spaces indicate deliminations between the cells. 空格表示单元之间的分隔。

Each "SkyFlyer_Leg*" needs a carrage return to take it onto a new line. 每个“ SkyFlyer_Leg *”都需要返回角点以将其移至新行。

These lines are being brought into the worksheet using another script that is parsing them from a CSV. 这些行将使用另一个脚本从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

This is what I have got so far, including the script that brings the code into this page. 到目前为止,这就是我所得到的,包括将代码带入此页面的脚本。 (Thanks to Shai Rado for the help so far) (感谢Shai Rado的帮助)

The result I want would have the data arranged like: 我想要的结果将数据安排如下:

18-Apr-17| 17年4月18日| 11:00:30 11:00:30

SkyFlyer1_Leg1| SkyFlyer1_Leg1 | 319437.222| 319437.222 | 146279.951| 146279.951 | -32.768 -32.768

SkyFlyer1_Leg2| SkyFlyer1_Leg2 | 319417.070| 319417.070 | 146268.105| 146268.105 | -32.768 -32.768

SkyFlyer1_Leg3| SkyFlyer1_Leg3 | 319410.548| 319410.548 | 146268.368| 146268.368 | -32.768 -32.768

Any help would be greatly appreciated. 任何帮助将不胜感激。

Cheers, J 干杯,J

Maybe this would work 也许这会工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM