View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
macropod[_5_] macropod[_5_] is offline
external usenet poster
 
Posts: 13
Default How to auto hide an empty row with VBA?

Hi,

Here's a macro that hides all rows that have non-numeric data, or zeros, in
Column B, and unhides them again if their status changes. Modifying it to
work a different range of columns and on a Change event basis (eg
Worksheet_Calculate or WorksheetChange) would be fairly trivial.

Sub CleanUp()
Dim CurrentRow As Long
Dim UsedRows As Range
On Error GoTo Abort
Set UsedRows = ActiveSheet.UsedRange.Rows
For CurrentRow = UsedRows.Rows.Count To 1 Step -1
If
Application.WorksheetFunction.Sum(UsedRows.Rows(Cu rrentRow).Columns("B:B"))
= 0 Then
UsedRows.Rows(CurrentRow).EntireRow.Hidden = True
Else UsedRows.Rows(CurrentRow).EntireRow.Hidden =
False
End If
Next CurrentRow
' If only hiding for printing purposes, use the next two lines to print or
preview then restore the worksheet
'ActiveWindow.SelectedSheets.PrintPreview
'ActiveSheet.Rows.EntireRow.Hidden = False
Abort:
End Sub



Change the 'Sum" parameter to "Count" if only non-numeric values are to be
used (i.e. zeros are left alone, but empty and text-only rows in the defined
columns are not).

Cheers


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.561 / Virus Database: 353 - Release Date: 13/01/2004