我有一个如下所示的数据集:
This1 GH This2 GH This3 GH This4 BR This5 BR This6 VB
当数据点发生变化时,即“GH”变为“BR”时,我希望 Excel 插入换行符。所以最终的数据看起来像这样。
This1 GH This2 GH This3 GH This4 BR This5 BR This6 VB
知道如何做到这一点吗?我认为负迭代 for 循环会起作用。但我不知道在这种情况下Excel将如何处理行操作。
最佳答案
最快的方法(经过尝试和测试)
Option Explicit
Sub Sample()
Dim aCell As Range, bCell As Range
Dim ExitLoop As Boolean
With Sheets("Sheet1")
.Columns("A:B").Subtotal GroupBy:=2, Function:=xlCount, TotalList:=Array(2), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Set aCell = .Cells.Find(What:=" Count", LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
.Rows(aCell.Row).ClearContents
Do While ExitLoop = False
Set aCell = .Cells.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
.Rows(aCell.Row).ClearContents
Else
ExitLoop = True
End If
Loop
End If
.Cells.RemoveSubtotal
End With
End Sub
我假设第 1 行有标题。
宏观行动
关于vba - 数据点更改后插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10539346/