Consulting

Results 1 to 16 of 16

Thread: Button to hide/unhide all empty rows? (Excel 97)

  1. #1

    Button to hide/unhide all empty rows? (Excel 97)

    Hello, I am very new to macros so please bear with me. I am trying to make a button to hide all the empty rows and another to unhide them for a report in Excel 97. I have tried using two command buttons but toggle would work too. In any case I am not sure how to proceed. I have code I have tried to adapt from another macro I found on this site that hides all empty rows upon activating the sheet. However I get a runtime error '1004' application-defined or object defined error near the bottom (red text). However I thought it was defined for hiddenrow? As it isn't working I must be wrong however I am not sure how to fix it.
    Any ideas would be greatly appreciated.

    Private Sub CommandButton2_Click()
    If CommandButton2.Value = True Then
    Dim HiddenRow&, RowRange As Range, RowRangeValue&
    Const FirstRow As Long = 4
    Const LastRow As Long = 180
    Const FirstCol As String = "A"
    Const LastCol As String = "O"
    ActiveWindow.DisplayZeros = False
    Application.ScreenUpdating = False
    For HiddenRow = FirstRow To LastRow
    Set RowRange = Range(FirstCol & HiddenRow & _
    ":" & LastCol & HiddenRow)
    RowRangeValue = Application.Sum(RowRange.Value)
    If RowRangeValue <> 0 Then
    Rows(HiddenRow).EntireRow.Hidden = False
    Else
    Rows(HiddenRow).EntireRow.Hidden = True
    End If
    Next HiddenRow
    Else
    Rows(HiddenRow).EntireRow.Hidden = False
    End If
    ActiveWindow.DisplayZeros = True
    Application.ScreenUpdating = True
    End Sub

    Thanks

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi ShadowMis
    Welcome to VBAX.
    If you select your code and click on the VBA button, it formats the code as shown.

    Try the following revision to your code. If you assign this to a Forms Toolbar button, this will act to toggle between hidden and visible rows.

    Sub HideRows()
    Dim HiddenRow&, RowRange As Range, RowRangeValue&, i As Long
    Const FirstRow As Long = 4
        Const LastRow As Long = 180
        Const FirstCol As String = "A"
        Const LastCol As String = "F"
    'Check for hidden rows
        For i = FirstRow To LastRow
            If Rows(i).Hidden = True Then
                Rows(FirstRow & ":" & LastRow).Hidden = False
                Exit Sub
            End If
        Next
    'Hide blank rows if none are hidden
        ActiveWindow.DisplayZeros = False
        Application.ScreenUpdating = False
        For HiddenRow = FirstRow To LastRow
            Set RowRange = Range(FirstCol & HiddenRow & _
            ":" & LastCol & HiddenRow)
            RowRangeValue = Application.WorksheetFunction.CountBlank(RowRange)
            If RowRangeValue <> Cells(1, LastCol).Column - Cells(1, FirstCol).Column + 1 Then
                Rows(HiddenRow).EntireRow.Hidden = False
            Else
                Rows(HiddenRow).EntireRow.Hidden = True
            End If
        Next HiddenRow
    ActiveWindow.DisplayZeros = True
        Application.ScreenUpdating = True
    End Sub
    Last edited by mdmackillop; 08-15-2005 at 01:11 PM. Reason: Amended to remove fixed value from code
    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'

  3. #3
    Thanks, I will get back to you to let you know how it works out.

  4. #4
    Humm.. It says I am Unable to set the hidden property for the range class (Runtime error 1004) for "Rows(FirstRow & ":" & LastRow).Hidden = False"

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try changing to the following
    Rows(FirstRow & ":" & LastRow).EntireRow.Hidden = False
    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'

  6. #6
    Shakes head, no that doesn't seem to work either...
    I think I am going to take a break on this macro and try some others that I want to do. Then maybe I will get some more experience and new insight that will help me with this one. I will let you know how it works out.
    Thanks for your help

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    It's probably an incompatability with Excel 97.
    Last edited by Ken Puls; 08-15-2005 at 11:19 PM. Reason: Removed suggestion to create new post.
    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'

  8. #8

    Excel 97 code required

    Anyone have some ideas about what might work on a dinosuar?
    Thanks
    Last edited by Ken Puls; 08-15-2005 at 11:19 PM. Reason: Removed reference to original post (now merged)

  9. #9
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    I ran that code on Excel 97 and it run's like a knife through butter.

    So something else must be the matter here...

    Could you post a test document that gives you the error?
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  10. #10
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Hi,
    my Excel97 brings such an error only when the sheet is locked, maybe that's the problem?

    Daniel

  11. #11
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi guys,

    Since this looks like it is a continuing discussion, I took the liberty of merging the threads and retitling it to reference the Excel 97 portion.

    Hope no one minds, but I also cut two comments to make the thread flow again.

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  12. #12
    Thank Kpuls
    Alright here is the report with the code, first run, as is, I get an error about not displaying zeros, so I removed it (which I don't want to do but for the sake of testing) then running it again, I get the unable to set hidden property error. Also, my report isn't locked. (At least I don't think it is locked, it isn't protected, or even shared)

  13. #13
    VBAX Regular
    Joined
    May 2004
    Location
    Adelaide, Australia
    Posts
    28
    Location
    This is a bug in xl97.
    If you were using a CommandButton you could set the TakeFocusOnClick Property to False.
    Since the ToggleButton does not have this property you will need to use the less preffered workaround of
    Activecell.Activate
    as the 1st line in the code.

    see:http://support.microsoft.com/default...&Product=xlw97

    (If you run the ToggleButton1_Click() sub directly from the VBE as it stands it will run fine, the problem is when run from the sheet & the button has the focus.)

  14. #14
    Wow. Thanks Insomniac, and Everyone! It is now working great and I think I understand it enough to do similar buttons on my other reports.

  15. #15
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hey Insomniac!

    Very nice catch!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Shadowmis
    Wow. Thanks Insomniac, and Everyone! It is now working great and I think I understand it enough to do similar buttons on my other reports.
    Glad to see you have it working now!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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