gpt4 book ai didi

vba - 数据点更改后插入行

转载 作者:行者123 更新时间:2023-12-02 18:58:31 24 4
gpt4 key购买 nike

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

This1    GHThis2    GHThis3    GHThis4    BRThis5    BRThis6    VB

当数据点发生变化时,即“GH”变为“BR”时,我希望 Excel 插入换行符。所以最终的数据看起来像这样。

This1    GHThis2    GHThis3    GHThis4    BRThis5    BRThis6    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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com