简体   繁体   English

将单个 Excel 行拆分为多行

[英]Splitting Single Excel Row to Multiple Rows

I'm working on a variation of this question:我正在研究这个问题的变体:
Split single row into multiple rows based on cell value in excel 根据excel中的单元格值将单行拆分为多行

In the old question, the poster wanted a new row to start with each cell that contained a specific value.在旧问题中,发帖人想要一个新行,以包含特定值的每个单元格开始。 I'm wondering if there is any way to start a new row after a cell that contains a specific value?我想知道是否有任何方法可以包含特定值的单元格之后开始新行?

I have adjusted the linked code for my use and it works perfectly, but it's creating a new row one cell too early for my data.我已经为我的使用调整了链接的代码,它工作得很好,但它为我的数据创建了一个新的第一行单元格为时过早。 The original question was about a year and a half old, and has an accepted answer so I didn't know if commenting on it would get a response.最初的问题大约有一年半的历史,并且有一个公认的答案,所以我不知道评论它是否会得到回应。

If that's not correct protocol for the site I apologize.如果这不是该网站的正确协议,我深表歉意。 Any help would be greatly appreciated!任何帮助将不胜感激!

Dim rngColLoop As Range
Dim rngSheet1  As Range
Dim wksSheet2  As Worksheet
Dim intColCounter  As Integer
Dim intRowCounter  As Integer

Sheets.Add.Name = "Sheet2"

With Worksheets("2014-2633")
    Set rngSheet1 = .Range(.Range("R1"), .Range("R1").End(xlToRight))
End With

Set wksSheet2 = Worksheets("Sheet2")
intRowCounter = 1
intColCounter = 0

wksSheet2.Range("A1").CurrentRegion.Clear

With rngSheet1
    For Each rngColLoop In .Columns
        If Trim(rngColLoop) <> "" Then
            If (Trim(rngColLoop)) <> "TennesseeSigned" Then
                intColCounter = intColCounter + 1
                wksSheet2.Cells(intRowCounter, intColCounter) = rngColLoop
            ElseIf (Trim(rngColLoop)) = "TennesseeSigned" Then
                intRowCounter = intRowCounter + 1
                intColCounter = 1
                wksSheet2.Cells(intRowCounter, intColCounter) = rngColLoop
            End If
        End If
    Next rngColLoop
End With

Set rngColLoop = Nothing
Set rngSheet1 = Nothing
Set wksSheet2 = Nothing

A simplistic approach is to detect the change and set a Boolean variable.一个简单的方法是检测变化并设置一个布尔变量。 Then test this variable to do the change, in the next loop cycle, like so:然后在下一个循环周期中测试此变量以进行更改,如下所示:

Dim rngColLoop As Range
Dim rngSheet1  As Range
Dim wksSheet2  As Worksheet
Dim intColCounter  As Integer
Dim intRowCounter  As Integer
Dim isNewRowRequired As Boolean: isNewRowRequired = False

Sheets.Add.Name = "Sheet2"

With Worksheets("2014-2633")
    Set rngSheet1 = .Range(.Range("R1"), .Range("R1").End(xlToRight))
End With

Set wksSheet2 = Worksheets("Sheet2")
intRowCounter = 1
intColCounter = 0

wksSheet2.Range("A1").CurrentRegion.Clear

With rngSheet1
    For Each rngColLoop In .Columns
        If Trim(rngColLoop) <> "" Then
            'See if the Bolean was set to true in the previous loop cycle
            If isNewRowRequired Then
                intRowCounter = intRowCounter + 1
                intColCounter = 1
                wksSheet2.Cells(intRowCounter, intColCounter) = rngColLoop
                isNewRowRequired = False
            Else
                'If not, carry on as usual (even if the cell value is matching)
                intColCounter = intColCounter + 1
                wksSheet2.Cells(intRowCounter, intColCounter) = rngColLoop
            End If
            'If value is matching, now set the Boolean to true 
            'so the break can be made in the next loop cycle
            If (Trim(rngColLoop)) = "TennesseeSigned" Then
                isNewRowRequired = True
            End If
        End If
    Next rngColLoop
End With

Set rngColLoop = Nothing
Set rngSheet1 = Nothing
Set wksSheet2 = Nothing

Hope this helps.希望这可以帮助。

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

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