Consulting

Results 1 to 5 of 5

Thread: Need help with macro to only unhide 5 rows at a time based on cell value

  1. #1
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    3
    Location

    Need help with macro to only unhide 5 rows at a time based on cell value

    I am new to VBA and macros...I have written a macro to unhide rows in a form when the cell value = 1, but I would like to limit it to only unhiding 5 rows at a time to speed up processing time (it would be rare to need more than 5 rows at a time for this form).

    I am using a For Each...Next Loop in my current code, but am running stuck. Any help would be appreciated.

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If you post the code, it would be easier to see whats going on.

  3. #3
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    3
    Location
    Here's the code:

    Sub UnProcessData()
    Dim oRow As Range

    With ActiveSheet

    For Each oRow In .UsedRange.Rows
    If .Cells(oRow.Row, "J").Value = 1 Then
    oRow.EntireRow.Hidden = False
    Else
    oRow.EntireRow.Hidden = False
    End If
    Next oRow

    End With

    End Sub

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The code you posted unhides all the rows in .UsedRange. That didn't sound like what you described so, I modified it so that only those rows with a 1 in column J are unhidden. Otherwise the row's visiblity is unchanged.
    This also sets the screenupdating to False while the routine runs. That makes the code run faster and avoids screen flicker.

    A counter was added, as soon as five rows are unhidden, the routine stops.

    Sub UnProcessData()
    Dim oRow As Range
    Dim counter As Integer
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        
        For Each oRow In .UsedRange.Rows
            If .Cells(oRow.Row, "J").Value = 1 Then
                oRow.EntireRow.Hidden = False
                counter = counter + 1
                If counter = 5 Then Exit For
            End If
        Next oRow
        
    End With
    
    Application.ScreenUpdating = True
    End Sub

  5. #5
    VBAX Newbie
    Joined
    Jun 2007
    Posts
    3
    Location
    Thanks, That helps alot!!

Posting Permissions

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