Excel VBA Worksheet Calculate running slowly

mcmahobt

Board Regular
Joined
Sep 2, 2014
Messages
55
Hello all,

My question pertains to the Worksheet_Calculate VBA sub. I have the code below that I wish to execute every time a cell is "confirmed" within the worksheet.

Basically, I have the table range that spans columns A:I, with various field headers. Column A has numbers that either contain integers or values with decimals in them. The current code cycles through column A, and if the cell value contains a decimal, it is formatted as white fill with black bordering and font. If it does not contain a decimal, it is formatted as blue fill with white font, etc.

The current code works, but is very slow to run. I am hoping for some guidance on getting a more consolidated/streamlined block of code for this...something that may include a "For Each Cell in Rng" loop?

Any help is appreciated!

Code:
Private Sub Worksheet_Calculate()

Application.ScreenUpdating = False


Dim ws As Worksheet, Rng As Range, LR As Integer, Lc As Integer, Cell As Range, r As Integer
Set ws = Application.ActiveWorkbook.Worksheets("Sheet1"): LR = ws.Range("A" & Rows.Count).End(xlUp).row:
Set Rng = ws.Range(Cells(4, 1), Cells(LR, 1))


For r = 4 To LR
    If InStr(Cells(r, 1).Value, ".") <> 0 Then
        With ws.Range(Cells(r, 1), Cells(r, 9))
            .Borders.LineStyle = xlContinuous
            .Borders.Weight = xlThin
            .Borders.ColorIndex = xlAutomatic
            .Interior.Color = RGB(255, 255, 255)
            .Font.Color = vbBlack
        End With
    Else
        With Range(Cells(r, 1), Cells(r, 9))
            .Borders.LineStyle = xlContinuous
            .Borders.Weight = xlThin
            .Borders.ColorIndex = xlAutomatic
            .Interior.Color = RGB(31, 73, 125)
            .Font.Color = vbWhite
        End With
    End If
Next r


Application.ScreenUpdating = True


End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Is there a reason why you go trough all cells, not only the one that is changed?

Because if you run this code on cell change ("Worksheet_Change") and target the active cell it will execute the code only there and would run a lot quicker.
 
Upvote 0
Is there a reason why you go trough all cells, not only the one that is changed?

Because if you run this code on cell change ("Worksheet_Change") and target the active cell it will execute the code only there and would run a lot quicker.

I was hoping to do just that at first, but the integer/decimal values are calculated through a formula which changes based on entries that can occur when rows are inserted into the table, so I need the entire table to auto-update on the change event, not just the last row.
 
Upvote 0
Please read the forum rules on cross-posting and comply with them in future. Thanks. :)
 
Upvote 0
I was hoping to do just that at first, but the integer/decimal values are calculated through a formula which changes based on entries that can occur when rows are inserted into the table, so I need the entire table to auto-update on the change event, not just the last row.

Try running it manually not from "Calculate" event.
If then it runs a lot quicker, then I would suggest to running it from a button.
Because I got a feeling that piece of code is run 186 times even if you just change 1 cell, as it loops trough the range and each cell change in turn triggers another calculation event.

But I might be wrong.


EDIT:
Just did a little test and it did run the code multiple times as the sheet recalculated it self every time a single cell was changed.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,203
Members
448,951
Latest member
jennlynn

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