vba - 数据点更改后插入行

标签 vba excel

我有一个如下所示的数据集:

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 行有标题。

宏观行动

enter image description here

关于vba - 数据点更改后插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10539346/

相关文章:

excel - 确定输入的字符串是否带有前导单引号

python 在列中搜索一个字符串,并从同一行返回另一个列值

vba - 使用 Excel VBA 列出特定模式的文件

vba - 重载属性

Excel 2007 - 公式更改为 #REF

string - vba中excel的application.evaluate命令的问题

excel - 想用 VBA 列出内置函数

arrays - Excel VBA将多个组合框值传递给动态数组

excel - Excel 中的自动分组/合并

excel - 从另一个 Excel 宏运行 Excel VBA 宏-错误 : Can't find PERSONAL. XLSB