+ Reply to Thread
Results 1 to 28 of 28

Auto Hiding rows based on range/data present or not present.

Hybrid View

  1. #1
    Registered User
    Join Date
    12-29-2012
    Location
    *
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Auto Hiding rows based on range/data present or not present.

    Ignore the first post. Take the last attachment and you can see the code there. First post was just an example where the attachment has a live version. The idea is that if there is any entry on the first row after the headers then that row along with headers will remain visible. If there is no data in the first row after the header then the header and data rows below will be hidden with the button.

    The current check on the update button is checking only the first row but when it goes to the next rows its only checking column a. Thats the problem right now. If there is any entry else where the entry is ignored and the row becomes hidden. Also please don't take any action with the data in A1. The formula in the calender requires that number.


    edit: to add more notes to clarify.
    Last edited by raze; 02-10-2013 at 09:38 AM.

  2. #2
    Forum Contributor
    Join Date
    01-13-2013
    Location
    Oklahoma
    MS-Off Ver
    Office 2007 / Office 2010
    Posts
    123

    Re: Auto Hiding rows based on range/data present or not present.

    Replace your current HideRows function with this:
    Sub HideRows()
        UpdateStatusBar "Adjusting the calendar"
        Application.ScreenUpdating = False
        Dim iStart As Long
        Dim iEnd As Long
        Dim iLast As Long
        Dim iRow As Long
        Dim sName As String
        ' get the sheet name to pass to the row and column functions
        sName = ActiveSheet.Name
        ' show all hidden rows
        ActiveSheet.Rows.Hidden = False
        ' get the last row on the sheet
        iLast = GetLastRow(sName)
        ' loop through all the groups checking them, change 11 to the group size if it changes in the future
        ' the first group starts at 7 so change if it changes in the future
        For iStart = 7 To iLast Step 11
            ' set the start and end rows for the data, change the + 10 if the group size changes in the future
            iRow = iStart + 2
            iEnd = iStart + 10
            ' if the first row has data, hide only the empty rows
            If GetLastColumnByRow(sName, iRow) > 0 Then
                ' loop through the rows
                For iRow = (iStart + 2) To iEnd Step 1
                    ' if the row is blank, hide it
                    If GetLastColumnByRow(sName, iRow) = 0 Then
                        ActiveSheet.Rows(iRow).Hidden = True
                    End If
                Next
            ' else hide the group
            Else
                ActiveSheet.Rows(iStart & ":" & iEnd).Hidden = True
            End If
        Next
        Application.ScreenUpdating = True
        UpdateStatusBar "Finished adjusting the calendar"
    End Sub
    
    Function GetLastRow(sSheet As String) As Long
        On Error GoTo NoRow
        Dim iReturn As Long
        iReturn = Worksheets(sSheet).Cells.Find("*", Searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
        GoTo FinishUp
    NoRow:
        iReturn = 0
        GoTo FinishUp
    FinishUp:
        GetLastRow = iReturn
    End Function
    
    Function GetLastColumnByRow(sSheet As String, iRowToUse As Long) As Long
        On Error GoTo NoCol
        Dim iReturn As Long
        iReturn = Sheets(sSheet).Rows(iRowToUse).Find(What:="*", LookIn:=xlValues, LookAt:=xlWhole, Searchorder:=xlByColumns, SearchDirection:=xlPrevious).Column
        GoTo FinishUp
    NoCol:
        iReturn = 0
        GoTo FinishUp
    FinishUp:
        GetLastColumnByRow = iReturn
    End Function
    
    Sub UpdateStatusBar(sText As String)
        Application.DisplayStatusBar = True
        Application.StatusBar = sText
    End Sub
    I like to use the status bar to show information about what is currently happening to the user. You can remove it if you want.
    Last edited by Yraen; 02-10-2013 at 10:28 AM. Reason: Added a comment I missed

+ 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