Consulting

Results 1 to 13 of 13

Thread: Macro to hide blank rows in Workbook

  1. #1

    Macro to hide blank rows in Workbook

    Halloo

    I have 15 work sheets and i put a VB code to hide the blank rows or the row if no value values. That code works fine. But the matter is i have 15 macros. i want my this code should run with all 15 sheet instead of having the 15 macros. Code is below.

    [VBA]
    Sub HideRowIfZeroInQ()
    Dim R As Range
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "O").End(xlUp).Row
    If LastRow > 800 Then LastRow = 800
    For Each R In Range("O9:O" & CStr(LastRow))
    If R.Value = 0 And R.Value <> "Q" Then R.EntireRow.Hidden = True
    Next

    End Sub
    [/VBA]

    Note :- When i run this code its work fine with me but very slow, if some one add some feature to make it fast

    Thanks for help

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Sub HideZeroQAllSheets()
        Dim oneSheet As Worksheet
        Application.ScreenUpdating = False
        For Each oneSheet In ThisWorkbook.WorkSheets
            Call HideRowIfZeroQ(oneSheet)
        Next oneSheet
        Application.ScreenUpdating = True
    End Sub
     
    Sub HideRowIfZeroQ(oneWorksheet As Worksheet)
        Dim R As Range, lastrow As Long
        With oneWorksheet.Range("o1:o800")
            For Each R In Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
                R.EntireRow.Hidden = (CStr(R.Value) = "0")
            Next R
        End With
    End Sub
    Last edited by mdmackillop; 04-06-2008 at 02:25 AM. Reason: ThisWorkbook.WorkSheets amended

  3. #3
    thank you mikerickson

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Bobby,
    If this is Soved you can mark it so using the Thread Tools dropdown
    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'

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Make this change to avoid an error if you have any chart sheets
    [VBA]
    For Each oneSheet In ThisWorkbook.Worksheets
    [/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'

  6. #6
    mikerickson i don't have any chart sheet in this work sheet

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    i don't have any chart sheet in this work sheet
    Perhaps not, but others may, who make use of this solution.
    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
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Ok tell me if i can attache this code with button in work sheet
    Getting a command buttons from the Forms Menu and assigning it to this macro would be one way to activate the routine.

    With all those rows hiding, the Properties of the command Buttons should be set to "Don't move or size with cells".

    Since it works across different worksheets, you might want to use Tools>Customize>Command bars to add a Macro button to the command bar so you can activate this from any sheet.

    mdmackillop, good catch.

  9. #9
    Dear mikerickson

    Thank you for your help, while running in code can i show a msg box in between "pls Wait while Processing". when code complete then "successfully Done".



    [VBA]
    Sub HideZeroQAllSheets()
    Dim oneSheet As Worksheet
    Application.ScreenUpdating = False
    For Each oneSheet In ThisWorkbook.WorkSheets
    Call HideRowIfZeroQ(oneSheet)
    Next oneSheet
    Application.ScreenUpdating = True
    End Sub

    Sub HideRowIfZeroQ(oneWorksheet As Worksheet)
    Dim R As Range, lastrow As Long
    With oneWorksheet.Range("o1:o800")
    For Each R In Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    R.EntireRow.Hidden = (CStr(R.Value) = "0")
    Next R
    End With
    End Sub
    [/VBA]

  10. #10
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Bobby_793, when pasting code please be sure to use the code tags as i have done on your thread, this is done by highlighting the code and clicking the Green VBA square in at the top of your new post window.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  11. #11
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    A MsgBox "Please wait while processing" would delay start of the processing until the user dismissed the box.

    A MsgBox "Done" requires that a useless key be pressed.

    If one were to put them in, it would be before setting the ScreenUpdating to False and after it were reset to True.

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You could do this with a userform. Add a userform with a label and try
    [VBA]
    Sub HideZeroQAllSheets()
    Dim oneSheet As Worksheet
    UserForm1.Show False
    UserForm1.Label1.Caption = "Please wait"
    Application.ScreenUpdating = False
    For Each oneSheet In ThisWorkbook.Worksheets
    Call HideRowIfZeroQ(oneSheet)
    Next oneSheet
    Application.ScreenUpdating = True
    UserForm1.Label1.Caption = "Finished"
    tim = Timer
    Do Until Timer > tim + 2
    DoEvents
    Loop
    Unload UserForm1
    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'

  13. #13
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    mdmackillop,

    Sweet!

    I tend to overlook modeless userforms.

Posting Permissions

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