Excel VBA speed up code to hide rows/delete color

mrwad

New Member
Joined
Oct 16, 2018
Messages
49
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".


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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Maybe the below is some improvement (untested)...

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

Test on a copy of your worksheet
 
Upvote 0
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".


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
There may be a way to dramatically sped this code up, but what I am thinking about depends on what results your formulas are outputting when they are not outputting the empty text string ("")... are they outputting numbers (this includes dates) only that is, no text values)?
 
Last edited:
Upvote 0
There may be a way to dramatically sped this code up, but what I am thinking about depends on what results your formulas are outputting when they are not outputting the empty text string ("")... are they outputting numbers (this includes dates) only that is, no text values)?

There are different types of values text and numbers. So some of the cells are outputting text and some numbers.
 
Upvote 0
There are different types of values text and numbers. So some of the cells are outputting text and some numbers.
Then what I had in mind won't work. I can give you one suggestion though. You can take this line of code out of the For..Next loop and execute it just before or after the loop...

myRange.Interior.ColorIndex = 0
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top