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
Excel-VBA Insert five empty rows below each group of name
提问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?谢谢。
回答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 1
in Cells()
is the column index, i.e. 1 == A
该1
中Cells()
是列指数,即1 == A