Delete every four rows in excel

2019-07-17 16:25发布

Very new to VBA but have experience in some other languages but need help with this one. I'm trying to create a macro that deletes every 4 rows. For example leave row 1 but delete 2,3,4,5 skip 6 and continue on. This is what I have so far:

Sub Macro1()

For x = 100 To 1 Step -2
    Range(x & ":" & x).Select
    Selection.ClearContents
Next x
End Sub

I'm not sure if my For loop is missing something or maybe i need some sort of index counter to tell it to delete four at a time.

标签: excel vba
1条回答
一夜七次
2楼-- · 2019-07-17 16:57

This will start at row 100 and step backwards to row 1.

Sub remove_rows()
Dim x As Long
Application.ScreenUpdating = False

For x = 100 To 1 Step -5
    Range(x & ":" & x - 3).EntireRow.Delete
Next x

Application.ScreenUpdating = True
End Sub

(I have the .Select in this .gif just to show you visually. You don't want that in the actual macro as it can slow it down): enter image description here

Edit: Ah, I noticed I delete the row. You can just replace .EntireRow.Delete with .EntireRow.ClearContents and it'll just clear the contents without deleting the row itself.

查看更多
登录 后发表回答