Would I need to reference a specific sheet?
It depends on where the VBA code is placed and when it is run.
If you leave sheet references off of your range, it will default to whatever the active sheet is at the time that it runs.
You can specify the sheet by either selecting it first, i.e.
VBA Code:
Sheets("Sheet1").Activate
or by adding sheet references to your range, i.e.
VBA Code:
Sheets("Sheet1").Range("B25:B27")
how would I make this happen automatically without having to run the macro each time?
It depends. Automatically based on what?
There is automated code in VBA called "Event Procedure" code, but it is dependent on some action happening, such as the update of a specific cell, the selection of a specific range, a calculation happening, a workbook opening or closing, etc.
Chip Pearson did quite a big write-up on them some years ago:
Events In Excel VBA
Also, how could I do this for B35-B127? But there are other headers in between B35-B127.
If there is some way to identify/differentiate the headers from the other data, that could be incorporated in the code.
For example, if the headers were always exactly every 7 rows.
Or if they started with the same word.
Or maybe in your case, the cells have a different background color.
For example:
VBA Code:
Dim cell As Range
For Each cell In Range("B35:B127")
' Check to make sure cell is not colored in specific blue color
If cell.Interior.Color <> 16737792 Then
' Check to see if cell is empty/blank
If cell.Value = "" Then
Rows(cell.Row).Hidden = True
Else
Rows(cell.Row).Hidden = False
End If
End If
Next cell
Note that in order to get the color code for any cell, you can use a little procedure like this:
VBA Code:
Sub GetColorCode()
MsgBox Range("A11").Interior.Color
End Sub
Just change the "A11" to the address of the cell you want to get the color code for, then run this code, and the code will pop-up on the screen (and you can use that number in your code).