Consulting

Results 1 to 4 of 4

Thread: Solved: Hiding Unused Rows

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Hiding Unused Rows

    I came across the code below on this forum. It is designed to hide blank rows within a defined range of rows and columns. Is there any way to expand this code to hide only those rows that are actually blank and leave visible cells that are blank as a result of a formula?

    [VBA]
    Option Explicit

    Private Sub Worksheet_Activate()

    Dim HiddenRow&, RowRange As Range, RowRangeValue&

    '*****************************
    '< Set the 1st & last rows to be hidden >
    Const FirstRow As Long = 4
    Const LastRow As Long = 20

    '< Set the columns that may contain data >
    Const FirstCol As String = "B"
    Const LastCol As String = "G"
    '*****************************

    ActiveWindow.DisplayZeros = False
    Application.ScreenUpdating = False

    For HiddenRow = FirstRow To LastRow

    '(we're using columns B to G here)
    Set RowRange = Range(FirstCol & HiddenRow & _
    ":" & LastCol & HiddenRow)

    'sums the entries in cells in the RowRange
    RowRangeValue = Application.Sum(RowRange.Value)

    If RowRangeValue <> 0 Then
    'there's something in this row - don't hide
    Rows(HiddenRow).EntireRow.Hidden = False
    Else
    'there's nothing in this row yet - hide it
    Rows(HiddenRow).EntireRow.Hidden = True
    End If

    Next HiddenRow

    Application.ScreenUpdating = True

    End Sub
    [/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Worksheet_Activate()
    Dim HiddenRow&, RowRange As Range, RowRangeValue&

    '*****************************
    '< Set the 1st & last rows to be hidden >
    Const FirstRow As Long = 4
    Const LastRow As Long = 20

    '< Set the columns that may contain data >
    Const FirstCol As String = "B"
    Const LastCol As String = "G"
    '*****************************

    ActiveWindow.DisplayZeros = False
    Application.ScreenUpdating = False

    For HiddenRow = FirstRow To LastRow

    '(we're using columns B to G here)
    Set RowRange = Range(FirstCol & HiddenRow & _
    ":" & LastCol & HiddenRow)

    'sums the entries in cells in the RowRange
    RowRangeValue = Application.CountIf(RowRange, "<>")

    Rows(HiddenRow).Hidden = (RowRangeValue = 0)
    Next HiddenRow

    Application.ScreenUpdating = True

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you only need one column try
    [VBA]
    Sub HideBlanks()
    On Error Resume Next
    Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks, guys. I appreciate the help!

    Opv

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •