Excel-VBA 在每组名称下方插入五个空行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5856677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:08:40  来源:igfitidea点击:

Excel-VBA Insert five empty rows below each group of name

excel-vbavbaexcel

提问by Kwai Lum

I'm in the middle of trying to do the above but fail. referring to the image below, i need to insert five empty rows below the 4th row (john lee) and another five empty rows below 7th row (bryan key) and another five below 9th row (casey carton) and so on, i got 30+ groups of different name to do. wondering how to write vba for this? thanks.

我正在尝试执行上述操作但失败了。参考下图,我需要在第 4 行(john lee)下方插入 5 个空行,在第 7 行下方插入另外 5 个空行(bryan key),在第 9 行下方插入另外 5 个空行(凯西纸箱)等等,我得到 30 + 不同名称的群组要做。想知道如何为此编写vba?谢谢。

name

姓名

回答by Alex K.

So 5 blanks after a change in sorted column A?

那么排序 A 列更改后有 5 个空白?

Const blanks = 5
Dim lastValue As String, i As Long, r As Long
Do
    r = r + 1
    If r > 1 And lastValue <> Cells(r, 1).Value Then
        If Cells(r, 1).Value = "" Then Exit Do
        For i = 1 To blanks
            Rows(r).Insert Shift:=xlDown
        Next
        r = r + blanks
    End If
    lastValue = Cells(r, 1).Value
Loop

the 1in Cells()is the column index, i.e. 1 == A

1Cells()是列指数,即1 == A