将单个Excel行拆分为多行

我正在研究这个问题的一个变种:
根据excel中的单元格值将单行分割成多行

在老问题中,海报想要一个新的行开始,每个单元格包含一个特定的值。 我想知道是否有任何方式 ,包含一个特定的值的单元格开始一个新的行?

我已经调整了我使用的链接代码,它完美的工作,但它为我的数据太早创build一个单元格的新行。 原来的问题是一年半左右,并且有一个可以接受的答案,所以我不知道评论这个答案是否会得到答复。

如果这是不正确的网站协议,我表示歉意。 任何帮助将不胜感激!

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 

一个简单的方法是检测变化并设置一个布尔variables。 然后testing这个variables,在下一个循环中进行更改,如下所示:

 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 

希望这可以帮助。