Slow code

InaCell

Board Regular
Joined
Feb 2, 2010
Messages
189
Hi

I have some code that works in 2 parts and is separated into 2 macros that are both called from a third.

The first macro selects the cells and clears the data within. These cells contain a formula which displays either TRUE or FALSE.

The second macro selects the cells and removes/clears the background colour if it exists.

Problem. The code is extremely slow. Could anyone improve the speed?

Code:
Sub RemoveCode()
    Application.ScreenUpdating = False ' speed up code for macro
        Call RemoveTrueFalse
        Call RemoveColour
    Application.ScreenUpdating = True ' set back to normal
End Sub
Sub RemoveTrueFalse()
Dim x As Long, a As Long, b As Long
    Application.ScreenUpdating = False ' speed up code for macro
Application.EnableEvents = False
    For a = 3 To 27 Step 3 'selects columns C F I L O R U
        x = Cells(Rows.Count, a).End(xlUp).Row ' sets last row of source range
        For b = 7 To x    'source range starts at row 7 & goes to bottom
                Cells(b, a).Value = "" 'remove all data from cell
     
        Next b 'goes to next Row in source sheet
    Next a 'goes to next Col in source sheet
Application.EnableEvents = True
    Application.ScreenUpdating = True ' set back to normal
End Sub
Sub RemoveColour()
Dim x As Long, a As Long, b As Long
    Application.ScreenUpdating = False ' speed up code for macro
Application.EnableEvents = False
    For a = 1 To 25 Step 3 'selects columns A D G J M P S
        x = Cells(Rows.Count, a).End(xlUp).Row ' sets last row of source range
        For b = 7 To x    'source range starts at row 7 & goes to bottom
    Cells(b, a).Select
       With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
         Next b 'goes to next Row in source sheet
    Next a 'goes to next Col in source sheet
Application.EnableEvents = True
    Application.ScreenUpdating = True ' set back to normal
End Sub

Thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Why do each cell separately from 7 to x? That is a contiguous range, so you can change the contents and formats in a single command instead of a loop.
 
Upvote 0
Hi Glenn

Thanks for all your assistance on my previous questions.

In this instance each "format" is in a different column. ie colours are in col A D G J M P S, while the formula is in Col C F I L O R U.

I am modifying some existing code that I had used and worked my head around. I am a bit lost on how to change it. Yes, it is contiguous but would I then have to do it for each col?

Thanks

Bernie
 
Upvote 0
It was my pleasure to help last time.

For your current problem ... sure you need to loop for each column, but then you are looping through each cell in that column from row 7 to x, when that chunk of cells is all in one lump ... i.e. don't do the 7 to x loop, just set the range to modify at that point. Like:
Code:
        x = Cells(Rows.Count, a).End(xlUp).Row ' sets last row of source range
     With Range(Cells(7, a),Cells(x,a)).Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
... at a guess.
 
Upvote 0
Have changed both macros so that each cell is not looped but each col in turn. Code still runs very slow.

Code:
Sub RemoveCode()
    Application.ScreenUpdating = False ' speed up code for macro
    Application.EnableEvents = False
        Call RemoveTrueFalse
        Call RemoveColour
    Application.EnableEvents = True
    Application.ScreenUpdating = True ' set back to normal
End Sub
Sub RemoveTrueFalse()
Dim x As Long, a As Long, b As Long
    Application.ScreenUpdating = False ' speed up code for macro
    Application.EnableEvents = False
        For a = 3 To 27 Step 3 'selects columns C F I L O R U
            x = Cells(Rows.Count, a).End(xlUp).Row ' sets last row of source range
        For b = 7 To x    'source range starts at row 7 & goes to bottom
                Range(Cells(7, a), Cells(x, a)).Value = "" 'remove all data from cell
            Next b 'goes to next Row in source sheet
        Next a 'goes to next Col in source sheet
    Application.EnableEvents = True
    Application.ScreenUpdating = True ' set back to normal
End Sub
Sub RemoveColour()
Dim x As Long, a As Long, b As Long
    Application.ScreenUpdating = False ' speed up code for macro
    Application.EnableEvents = False
        For a = 1 To 25 Step 3 'selects columns A D G J M P S
            x = Cells(Rows.Count, a).End(xlUp).Row ' sets last row of source range
        For b = 7 To x    'source range starts at row 7 & goes to bottom
                With Range(Cells(7, a), Cells(x, a)).Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
            Next b 'goes to next Row in source sheet
        Next a 'goes to next Col in source sheet
    Application.EnableEvents = True
    Application.ScreenUpdating = True ' set back to normal
End Sub

Is there anything else that may be causing this?
 
Upvote 0
Don't turn screenupdating and events on and off within the two called procedures, and you are still looping:

Code:
Sub RemoveTrueFalse()
Dim x As Long, a As Long, b As Long

        For a = 3 To 27 Step 3 'selects columns C F I L O R U
            x = Cells(Rows.Count, a).End(xlUp).Row ' sets last row of source range
                Range(Cells(7, a), Cells(x, a)).Value = "" 'remove all data from cell
        Next a 'goes to next Col in source sheet
End Sub
Sub RemoveColour()
Dim x As Long, a As Long, b As Long

        For a = 1 To 25 Step 3 'selects columns A D G J M P S
            x = Cells(Rows.Count, a).End(xlUp).Row ' sets last row of source range
                With Range(Cells(7, a), Cells(x, a)).Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
        Next a 'goes to next Col in source sheet

End Sub
 
Last edited:
Upvote 0
You might also turn off Calculation along with Events and ScreenUpdating.

Also, since you're disabling Screenupdating and Events in the first sub, there's no need to do it in the other 2 subs...

Try

Code:
Sub RemoveCode()
Dim PrevCalc As Variant
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    PrevCalc = .Calculation
    .Calculation = xlCalculationManual
End With
 
Call RemoveTrueFalse
Call RemoveColour
 
With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = PrevCalc
End With
End Sub
 
Sub RemoveTrueFalse()
Dim x As Long, a As Long
 
For a = 3 To 27 Step 3 'selects columns C F I L O R U
    x = Cells(Rows.Count, a).End(xlUp).Row ' sets last row of source range
    Range(Cells(7, a), Cells(x, a)).Value = "" 'remove all data from cell
Next a 'goes to next Col in source sheet
 
End Sub
Sub RemoveColour()
Dim x As Long, a As Long
 
For a = 1 To 25 Step 3 'selects columns A D G J M P S
    x = Cells(Rows.Count, a).End(xlUp).Row ' sets last row of source range
    With Range(Cells(7, a), Cells(x, a)).Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
Next a 'goes to next Col in source sheet
End Sub
 
Last edited:
Upvote 0
jonmo1,

Would this line:
Code:
    Range(Cells(7, a), Cells(x, a)).Value = "" 'remove all data from cell
Be quicker as:
Code:
    Range(Cells(7, a), Cells(x, a)).ClearContents 'remove all data from cell
The second line removes the need for evaluation or to set a value per se? I imagine though the noticeable effect is next to negligible...
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
Lawrenceiow

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