问题
Below is vba code that I am using now. Edit details at the bottom.
Sub AddBlankRows()
'
Dim iRow As Integer
Range("a1").Select
'
iRow = 1
'
Do
'
If Cells(iRow + 1, 1) <> Cells(iRow, 1) Then
Cells(iRow + 1, 1).EntireRow.Insert shift:=xlDown
iRow = iRow + 2
Else
iRow = iRow + 1
End If
'
Loop While Not Cells(iRow, 2).Text = ""
'
End Sub
The above vba code I found (googling) in one of Stackoverflows questions that someone has asked, and it worked for my purpose. However I cannot locate it to give information as to which question it arose from.
I am now using it and works great. However I need more rows added. So I have to manually insert rows as I need them. I would like to make the vba code do it for me. Instead of adding 1 row after each grouped cell. I need 3 rows added. Could somebody help me edit the above macro to allow it to give me 3 rows rather than 1 row.
This is my first post and thanking everybody in advance. GraceSarah
回答1:
This would be the least invasive way to modify your existing formula to add 2 more rows
Sub AddBlankRows()
'
Dim iRow As Integer
Dim x as Integer
Range("a1").Select
'
iRow = 1
'
Do
'
If Cells(iRow + 1, 1) <> Cells(iRow, 1) Then
For x = 1 To 3
Cells(iRow + 1, 1).EntireRow.Insert shift:=xlDown
Next x
iRow = iRow + 4
Else
iRow = iRow + 1
End If
'
Loop While Not Cells(iRow, 2).Text = ""
'
End Sub
来源:https://stackoverflow.com/questions/19982706/add-3-rows-to-existing-vba-code