I'm trying to write a macro that will hide all rows where there is text in column B AND where the values in columns C through P are zero (not where the values sum to zero, but where each cell in those columns is zero).
I've written a macro that will hide the rows where the values in the cells in a given row in columns C through P are zero, but I'm having trouble adding in the second condition (checking if there is text in column B as well). Basically I have a spreadsheet with about 950 line items, where many of the rows have zeroes associated with a particular line item. But the line items fall into categories (such as personnel, programming, etc.) and these categories are separated by completely blank/empty rows. I don't want the macro to hide these empty rows, just the ones where there is a line item in column B and only values of zero in columns C through P. Make sense?
I'm using Excel 2007.
Here is what I have written so far, which just takes care of checking and hiding rows with values of zero in Columns C through P.
Code:
Option Explicit
Sub HideOnZero()
Dim rngTest As Range
Dim rngCell As Range
Dim lngStRow As Long
Dim lngEndRow As Long
Dim rngRow As Range
Dim blnZero As Boolean
Dim n As Integer
On Error GoTo ErrHnd
Application.ScreenUpdating = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
With ActiveSheet
'selected cell within required range
'set range to current region around selected cell
Set rngTest = Range("C9:U961")
'get rows in range
lngStRow = rngTest.Rows(1).Row
lngEndRow = lngStRow + rngTest.Rows.Count - 1
'go through each row
For n = lngStRow To lngEndRow
blnZero = True
'test each cell in the row from column C to column P
For Each rngCell In Range("C" & Format(n, "#0") & ": P" & Format(n, "#0")).Cells
'test if cell value is zero
If rngCell.Value <> 0 Then
blnZero = False
End If
'if a non-zero cell encountered don't test this row any more
If blnZero = False Then Exit For
'if we get to the last cell in this row (column P)
'then setup a range for the whole row
If rngCell.Column = Range("C" & Format(n, "#0") & ": P" & _
Format(n, "#0")).Columns.Count + 2 Then
Set rngRow = rngCell.EntireRow
End If
Next rngCell
'hide the row if no non-zero cells found
If blnZero = True Then
rngRow.Hidden = True
End If
Next n
End With
Exit Sub
'error handler
ErrHnd:
Err.Clear
End Sub
I've written a macro that will hide the rows where the values in the cells in a given row in columns C through P are zero, but I'm having trouble adding in the second condition (checking if there is text in column B as well). Basically I have a spreadsheet with about 950 line items, where many of the rows have zeroes associated with a particular line item. But the line items fall into categories (such as personnel, programming, etc.) and these categories are separated by completely blank/empty rows. I don't want the macro to hide these empty rows, just the ones where there is a line item in column B and only values of zero in columns C through P. Make sense?
I'm using Excel 2007.
Here is what I have written so far, which just takes care of checking and hiding rows with values of zero in Columns C through P.
Code:
Option Explicit
Sub HideOnZero()
Dim rngTest As Range
Dim rngCell As Range
Dim lngStRow As Long
Dim lngEndRow As Long
Dim rngRow As Range
Dim blnZero As Boolean
Dim n As Integer
On Error GoTo ErrHnd
Application.ScreenUpdating = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
With ActiveSheet
'selected cell within required range
'set range to current region around selected cell
Set rngTest = Range("C9:U961")
'get rows in range
lngStRow = rngTest.Rows(1).Row
lngEndRow = lngStRow + rngTest.Rows.Count - 1
'go through each row
For n = lngStRow To lngEndRow
blnZero = True
'test each cell in the row from column C to column P
For Each rngCell In Range("C" & Format(n, "#0") & ": P" & Format(n, "#0")).Cells
'test if cell value is zero
If rngCell.Value <> 0 Then
blnZero = False
End If
'if a non-zero cell encountered don't test this row any more
If blnZero = False Then Exit For
'if we get to the last cell in this row (column P)
'then setup a range for the whole row
If rngCell.Column = Range("C" & Format(n, "#0") & ": P" & _
Format(n, "#0")).Columns.Count + 2 Then
Set rngRow = rngCell.EntireRow
End If
Next rngCell
'hide the row if no non-zero cells found
If blnZero = True Then
rngRow.Hidden = True
End If
Next n
End With
Exit Sub
'error handler
ErrHnd:
Err.Clear
End Sub