Gantt Chart Conditional formatting Code slow

Brass Contributor

I have been using this below code for Conditional formatting to highlight the rows between dates as Gantt Chart works.

 

My code is perfect but it is too much slow. I want to make it work faster. Your help will be greatly appreciated.

 

Sub color()

Const DATE_RNG As String = "H7:RD7"

Dim ws As Worksheet, r As Long, pcent As Single
Dim rng As Range, color As Integer
Set ws = Sheet7

color = Sheet7.Range("K4").Interior.ColorIndex

Dim dtStart As Date, dtEnd As Date, dtEndBar As Date
Dim dur As Integer

For r = 12 To 2008
pcent = ws.Cells(r, "D")
dtStart = ws.Cells(r, "E")
dtEnd = ws.Cells(r, "F")
dur = DateDiff("d", dtStart, dtEnd) + 1

dtEndBar = DateAdd("d", Int(dur * pcent) - 1, dtStart)

For Each rng In ws.Range(DATE_RNG)

If rng >= dtStart And rng <= dtEndBar Then
ws.Cells(r, rng.Column).Interior.ColorIndex = color
Else
ws.Cells(r, rng.Column).Interior.ColorIndex = xlNone
End If

Next
Next

End Sub

 

0 Replies