+ Reply to Thread
Results 1 to 5 of 5

Fast way to make rows visible and invisible

  1. #1
    gimme_this_gimme_that@yahoo.com
    Guest

    Fast way to make rows visible and invisible


    My application has the following Sub which hides some rows and makes
    others visible :

    Is there a way to speed up this code somehow so that the cells are set
    to visible and invisible in two instructions.

    The code is real slow.

    Public Sub RegionView(sheet As Worksheet, NavigationColumn As Long)
    Dim i As Integer
    Dim rowType As String
    For i = STARTING_ROW_INDEX To MAXIMUM_ROW_INDEX
    rowType = sheet.Cells(i, NavigationColumn).Value
    If i < 2 Then
    MsgBox rowType
    End If
    If "br_br" = rowType Then
    sheet.Rows(CStr(i) + ":" + CStr(i)).EntireRow.Hidden = True
    ElseIf "br_ta" = rowType Then
    sheet.Rows(CStr(i) + ":" + CStr(i)).EntireRow.Hidden = True
    ElseIf "rg_rg" = rowType Then
    sheet.Rows(CStr(i) + ":" + CStr(i)).EntireRow.Hidden = True
    ElseIf "rg_tr" = rowType Then
    sheet.Rows(CStr(i) + ":" + CStr(i)).EntireRow.Hidden = False
    Else
    sheet.Rows(CStr(i) + ":" + CStr(i)).EntireRow.Hidden = False
    End If
    If i > MAXIMUM_ROW_INDEX Then
    Exit For
    End If
    Next i
    End Sub


  2. #2
    Ken Johnson
    Guest

    Re: Fast way to make rows visible and invisible

    Hi,
    Turning off screenupdating should speed things up (you'll still see
    your msgbox)
    Application.ScreenUpdating = False added at the start is enough for
    that (it's automatically turned back on when the code finishes)
    Rows(CStr(i) + ":" + CStr(i)).EntireRow.Hidden = True can be reduced
    to
    Rows(i).EntireRow.Hidden = True which reduces the number of vb
    functions used and should therefore speed things up as well.
    Ken Johnson


  3. #3
    gimme_this_gimme_that@yahoo.com
    Guest

    Re: Fast way to make rows visible and invisible

    Thanks Ken.

    I used your tip.


  4. #4
    Ken Johnson
    Guest

    Re: Fast way to make rows visible and invisible

    Hi,
    Thanks for the feedback. Hope you're getting reasonable speed now.
    ScreenUpdating usually makes a signifcant difference when the code is
    changing the active sheet.
    Ken Johnson


  5. #5
    gimme_this_gimme_that@yahoo.com
    Guest

    Re: Fast way to make rows visible and invisible

    This turned out to be what I needed ...

    Note that there is a limit to about 30 rows to how many rows can be
    selected at once using this syntax..

    Range(x1:x1,x2:x2...").Select




    Public Sub RegionView(sheet As Worksheet, NavigationColumn As Long)
    Dim i As Integer, j As Integer
    Dim rowType As String, hideRows As String, showRows As String
    hideRows = ""
    showRows = ""
    Dim LastRow As Integer
    LastRow = sheet.Cells(rows.Count, "A").End(xlUp).Row
    ' set all cells to visible
    sheet.Cells.EntireRow.Hidden = False

    j = 0
    For i = STARTING_ROW_INDEX To LastRow
    rowType = sheet.Cells(i, NavigationColumn).Value

    If "br_branch" = rowType Then
    j = j + 1
    hideRows = hideRows + CStr(i) + ":" + CStr(i) + ","
    ElseIf "br_target" = rowType Then
    j = j + 1
    hideRows = hideRows + CStr(i) + ":" + CStr(i) + ","
    ElseIf "rg_region" = rowType Then
    j = j + 1
    hideRows = hideRows + CStr(i) + ":" + CStr(i) + ","
    End If


    ' Excel returns a 400 error if more than 30 rows are hidden at a time
    If j > 30 Then
    hideRows = Left(hideRows, Len(hideRows) - 1)
    sheet.Range(hideRows).EntireRow.Hidden = True
    j = 0
    hideRows = ""
    End If

    Next i
    hideRows = Left(hideRows, Len(hideRows) - 1)
    sheet.Range(hideRows).EntireRow.Hidden = True

    End Sub


+ 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