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!
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