I'm writing a formatting macro for a report, but what I've come up with is not running nearly as quickly as I'd like. Any help or suggestions to increase speed would be greatly appreciated.
My thought when putting together the below code was that if I iterate through each row, and determine the ranges that need to have formatting applied to them merging into a single range using union(), and then apply the format at the end, it would be faster than applying the format for each row individually. I'm not sure this is the case, considering how long this code took to run for about 40k rows.
Dim FinalRowReport As Long
Dim i As Long
Dim rangeFormat As Range
Dim rangeBold As Range
Dim rangeColor As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
FinalRowReport = Cells(Rows.Count, 1).End(xlUp).Row
For i = 4 To FinalRowReport
If Cells(i, 2) = Cells(i - 1, 2) Then
If rangeColor Is Nothing Then
Set rangeColor = Range(Cells(i, 1), Cells(i, 12))
Else
Set rangeColor = Union(rangeColor, Range(Cells(i, 1), Cells(i, 12)))
End If
End If
If Right(Cells(i, 2).Value, 5) = "Total" Then
If rangeFormat Is Nothing Then
Set rangeFormat = Range(Cells(i, 1), Cells(i, 19))
Set rangeBold = Range(Cells(i, 20), Cells(i, 23))
Else
Set rangeFormat = Union(rangeFormat, Range(Cells(i, 1), Cells(i, 23)))
Set rangeBold = Union(rangeBold, Range(Cells(i, 20), Cells(i, 23)))
End If
End If
Next i
rangeColor.Font.Color = RGB(255, 255, 255)
rangeFormat.Interior.Color = RGB(217, 217, 217)
rangeFormat.Font.Color = RGB(217, 217, 217)
rangeBold.Interior.Color = RGB(217, 217, 217)
rangeBold.Font.Bold = True
With rangeFormat.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThick
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
The code does work - it's just extremely long, maybe like 20 minutes. Any help would be greatly appreciated.