There is something called a "For loop" that helps you go through a big number of rows without having to write down every single one of them.
Read about them here: https://www.excel-easy.com/vba/loop.html
I will give you a basic example:
Sub hideSomeRows()
Dim currentRow As Long
Dim lastRow As Long
lastRow = 1000 'or any other last row you want to check
For currentRow = 1 To lastRow Step 5
If Range("S" & currentRow).Value <= 0 Then
Rows(currentRow & ":" & currentRow + 4).EntireRow.Hidden = True
End If
Next currentRow
End Sub
This will loop through every 5th row between row 1 and row 1000 and hide itself and the next 4 rows if there is a negative value or zero in column S of the inspected row.
I kept the code as simple as possible, but you should really consider qualifying "Range" before using it. Here is a very good discussion of why it is important and how to do it.
Find some quick code snippets here showing how to define the workbook and the worksheet you are working on as variables in your code.
And here is in-depth information about working with worksheets:
The gist of it: Do
Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("!!YOURWORKSHEETNAME!!") 'replace !!YOURWORKSHEETNAME!! with the name of your worksheet
and then write ws.Range
instead of just Range
. Same goes for Rows.
Why you ask? Try looking at another worksheet before executing your macro and you will see, that your macro is going to always interpret Range
as a range of the last active worksheet if not told otherwise!