VBA keep every 6th row

urimagic

New Member
Joined
Jun 1, 2023
Messages
16
Office Version
  1. 2013
Platform
  1. Windows
Hi friends,

please would someone help me with a code to keep every 5th row, starting from row 1?..This list grows by the week and will most certainly reach the maximum rows of the sheet eventually..Thank you all kindly..
My Groups.xlsm
AB
1Kuilsriver Goods And Servicesđź’˛
2Your contribution points0
3Update time
4Aug 17, 2023 5:56:52pm
5Aug 17, 2023 5:56:52pm
6Table View - * Buy * Sell * Ads
7Your contribution points0
8Update time
9Aug 17, 2023 5:56:13pm
10Aug 17, 2023 5:56:13pm
11Stellenbosh Ads, Buy & Sell
12Your contribution points0
13Update time
14Aug 17, 2023 5:56:10pm
15Aug 17, 2023 5:56:10pm
16Constantia/Tokai/Bishops Court/Sweet Valley/Steenberg/Parel Valei Ads Exec
17Your contribution points0
18Update time
19Aug 17, 2023 5:55:11pm
20Aug 17, 2023 5:55:11pm
21Pinelands Notice Board
22Your contribution points0
23Update time
24Aug 17, 2023 5:54:50pm
25Aug 17, 2023 5:54:50pm
26Rondebosch Advertising
Sheet1
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,

maybe this will get you where you want. Note the rows will be deleted from bottom up, and therefore you will be left with just a list of rows you wanted to keep at the top of the page.
ie. It does not leave the rows in between blank - it removes them completely.

If you wanted to just "clear" the rows, use the second piece of code instead, as this will leave your untouched rows where they are.

(and anything in Col C across the columns will also be removed)
cheers
Rob

VBA Code:
Sub delete_rows()

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

For x = lastrow To 1 Step -1

If x Mod 5 <> 1 Then
    Rows(x).EntireRow.Delete
End If

Next x

End Sub

To Clear row only (not delete)
VBA Code:
Sub delete_rows()

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

For x = lastrow To 1 Step -1

If x Mod 5 <> 1 Then
    Rows(x).EntireRow.Clear
End If

Next x

End Sub
 
Upvote 0
Hi,

maybe this will get you where you want. Note the rows will be deleted from bottom up, and therefore you will be left with just a list of rows you wanted to keep at the top of the page.
ie. It does not leave the rows in between blank - it removes them completely.

If you wanted to just "clear" the rows, use the second piece of code instead, as this will leave your untouched rows where they are.

(and anything in Col C across the columns will also be removed)
cheers
Rob

VBA Code:
Sub delete_rows()

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

For x = lastrow To 1 Step -1

If x Mod 5 <> 1 Then
    Rows(x).EntireRow.Delete
End If

Next x

End Sub

To Clear row only (not delete)
VBA Code:
Sub delete_rows()

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

For x = lastrow To 1 Step -1

If x Mod 5 <> 1 Then
    Rows(x).EntireRow.Clear
End If

Next x

End Sub
Hi RobP,

Thank you very much. Your code works just great, although I have come to realize something, which is that for some reason the report adds a row at some point in the report, obviiously causing problems. So, I'm a bit stumped now as how we can do this....Was wondering, could the code be altered instead to delete all rows which do not contain bold text?..This will solve the issue?..please, if you don't mind...
My Groups.xlsm
AB
786Cape Town Business
787Your contribution points0
788Update time
789Aug 01, 2023 7:31:03pm
790Aug 01, 2023 7:31:03pm
791Cape town buying and selling group
792Your contribution points31
793How you earned contribution points
794Contribution points earned from interactions31
795Update time
796Aug 17, 2023 10:39:10pm
797Aug 01, 2023 7:30:59pm
798BUYING AND SELLING IN CAPETOWN
799Your contribution points0
800Update time
801Aug 01, 2023 7:30:32pm
802Aug 01, 2023 7:30:32pm
803Markets/Vendors Cape Town
Sheet1
 
Upvote 0
You could try something like this:
VBA Code:
Sub delete_rows()

LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

For x = LastRow To 1 Step -1

If Range("A" & x).Font.Bold = False Then
    Rows(x).EntireRow.Delete
End If

Next x

End Sub
 
Upvote 0
Solution
If you have a lot of lines of data, I would recommend suppressing screen updating until the end.
This will speed up your code a bit, i.e.
Rich (BB code):
Sub delete_rows()

Application.ScreenUpdating = False

LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

For x = LastRow To 1 Step -1

If Range("A" & x).Font.Bold = False Then
    Rows(x).EntireRow.Delete
End If

Next x

Application.ScreenUpdating = True

End Sub
 
Upvote 0
You could try something like this:
VBA Code:
Sub delete_rows()

LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

For x = LastRow To 1 Step -1

If Range("A" & x).Font.Bold = False Then
    Rows(x).EntireRow.Delete
End If

Next x

End Sub
Hi Candyman8019,

Code is perfect!!..I do appreciate, thank you very much!
 
Upvote 0
If you have a lot of lines of data, I would recommend suppressing screen updating until the end.
This will speed up your code a bit, i.e.
Rich (BB code):
Sub delete_rows()

Application.ScreenUpdating = False

LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

For x = LastRow To 1 Step -1

If Range("A" & x).Font.Bold = False Then
    Rows(x).EntireRow.Delete
End If

Next x

Application.ScreenUpdating = True

End Sub
Hi, Joe4,

Thanks, I have added your piece as well...much appreciated!..Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,157
Messages
6,123,341
Members
449,097
Latest member
thnirmitha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top