dcsimg
Results 1 to 4 of 4

Thread: Excel VBA speed up code to hide rows/delete color

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2018
    Posts
    10

    Excel VBA speed up code to hide rows/delete color

    I am using following code to delete color and hide rows that include several criteria before exporting sheets to pdf's. Is there any way to speed up this process as it is taking quite a lot of time to process. Especially in situations when I have several sheets in one workbook and to apply this on each sheet = "printed page". There are different types of values text and numbers. So some of the cells are outputting text and some numbers.

    Code:
      Sub Color()
            Dim myRange As Range
            Dim cell As Range
            Application.ScreenUpdating = False
            Set myRange = ThisWorkbook.Sheets("Print version").Range("Print_Area")
                For Each cell In myRange
                myRange.Interior.ColorIndex = 0
                If cell.HasFormula = True And cell.Value = "" And cell.EntireRow.Hidden = False Then Rows(cell.Row).EntireRow.Hidden = True
            Next
            Application.ScreenUpdating = True
        End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,280

    Re: Excel VBA speed up code to hide rows/delete color

    There is no need to have this inside the loop (where it runs multiple times, but will have no effect after the first):
    myRange.Interior.ColorIndex = 0
    The If statement can be made faster by changing it to this:
    Code:
                If cell.HasFormula Then If cell.Value = "" Then If cell.EntireRow.Hidden = False Then Rows(cell.Row).EntireRow.Hidden = True
    ...or to make it easier to read, spread it across multiple lines:
    Code:
                If cell.HasFormula Then
                   If cell.Value = "" Then
                      If cell.EntireRow.Hidden = False Then Rows(cell.Row).EntireRow.Hidden = True
                   End If
                End If
    There may be a better way to structure the loop so that once a row is hidden you don't need to check the other cells, but somebody else will need to help with that.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,819

    Re: Excel VBA speed up code to hide rows/delete color

    it is probably quicker to not to test if the row is not hidden, just set to hidden anyway if the other criteria match

    another option to speed up may be to use union to append all the ranges (rows) matching the criteria then hide the combined range at the after the loop, you would need to do some speed tests to confirm, it would have more effect on large ranges

    i have not tested this
    Code:
    Dim r As Range, e As Boolean, cel As Range
    For Each cel In myrange
        If IsEmpty(cel) And cel.HasFormula Then
            If e Then
                Set r = Union(r, cel)
                Else
                Set r = cel   ' for first row only when r is nothing
                e = True
            End If
        End If
    Next
    r.EntireRow.Hidden = True
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,500

    Re: Excel VBA speed up code to hide rows/delete color

    The way i'd do it

    Code:
    Dim bCellEmpty As Boolean
    bCellEmpty=False
    If cell.Value="" Then bCellEmpty=True
    
    Rows(cell.Row).EntireRow.Hidden=cell.HasFormula And bCellEmpty And Not cell.EntireRow.Hidden
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width