+ Reply to Thread
Results 1 to 6 of 6

Modify VBA code to hide blanks rows

Hybrid View

  1. #1
    Registered User
    Join Date
    05-04-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2011 OSX Home, Excel 2010 Home, Excel 2002 SP3 Work
    Posts
    29

    Modify VBA code to hide blanks rows

    Hi,

    I have a great code that I want to modify. The code hides rows if the sum of the row is smaller or larger than zero. But I want to modify the code so it hides the rows only if it is blank. So a row with a string of text in it will not get hidden.

    Sub Komprimera()
         
        Dim HiddenRow&, RowRange As Range, RowRangeValue&
         
         '*****************************
         '< Set the 1st & last rows to be hidden >
        Const FirstRow As Long = 4
        Const LastRow As Long = 65
         
         '< Set the columns that may contain data >
        Const FirstCol As String = "C"
        Const LastCol As String = "AZ"
         '*****************************
         
        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

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Modify VBA code to hide blanks rows

    hi, Ranew, change this line:

    RowRangeValue = Application.Sum(RowRange.Value)
    to this one

    RowRangeValue = Application.CountA(RowRange.Value)
    Last edited by watersev; 05-17-2011 at 08:13 AM.

  3. #3
    Registered User
    Join Date
    05-04-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2011 OSX Home, Excel 2010 Home, Excel 2002 SP3 Work
    Posts
    29

    Re: Modify VBA code to hide blanks rows

    I am making an excel file for come co-workers and it needs to be as clean and simple as possible. One way to remove unused rows would be to set a macro to a button. Clicking this button would hide all unused rows. A filter would not work as well. I only need to figure out how to change

    If RowRangeValue <> 0 Then

    to search for word and numbers.

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Modify VBA code to hide blanks rows

    hi, Ranew, I've amended my last post, try to change that line

  5. #5
    Registered User
    Join Date
    05-04-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2011 OSX Home, Excel 2010 Home, Excel 2002 SP3 Work
    Posts
    29

    Re: Modify VBA code to hide blanks rows

    watersev, a good step forward

    The code hides blank rows! Unfortunately I have alot of rows containing formulas, but returns blanks. I need to hide them aswell.

  6. #6
    Registered User
    Join Date
    05-04-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2011 OSX Home, Excel 2010 Home, Excel 2002 SP3 Work
    Posts
    29

    Re: Modify VBA code to hide blanks rows

    I found this;

    Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    'Hide rows with formulas but no data
    Dim cell As Range
    Application.ScreenUpdating = False
    With ActiveSheet.UsedRange
    .Rows.Hidden = False
    For Each cell In .Columns(1).SpecialCells(xlCellTypeFormulas)
    If cell.Value = "" Or cell.Value = 0 Then cell.entirerow.Hidden = True
    Next cell
    End With
    End Sub
    I don't know if it is of any help...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1