code to be placed in sheet object.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDates As Range
Dim strDiscipline As String
Dim rngStartDate As Range
Dim rngEndDate As Range
Dim lngStartCol As Long
Dim lngEndCol As Long
Dim lngColourIndex As Long
On Error GoTo ErrGantt
If Not Intersect(Range("G:H"), Target) Is Nothing Then
Set rngDates = Range("J1", Range("J1").End(xlToRight))
If Len(Target.Value) = 0 Then
' clear
Intersect(rngDates.EntireColumn, Target.Rows(1).EntireRow).Interior.ColorIndex = xlNone
Else
If Target.Column = 7 Then
' start date altered
Set rngStartDate = Target
Set rngEndDate = Target.Offset(, 1)
Else
' end date altered
Set rngEndDate = Target
Set rngStartDate = Target.Offset(, -1)
End If
With Application.WorksheetFunction
lngStartCol = .Match(.HLookup(rngStartDate, rngDates, 1, True), rngDates, 0)
lngEndCol = .Match(.HLookup(rngEndDate, rngDates, 1, True), rngDates, 0)
End With
'Math=Blue, Reading=Red, Social Studies=Pink and Science = Green
Select Case UCase(rngStartDate.Offset(, -5))
Case "MATH"
lngColourIndex = 41 ' Blue
Case "READING"
lngColourIndex = 3 ' Red
Case "SOCIAL STUDIES"
lngColourIndex = 7 ' Pink
Case "SCIENCE"
lngColourIndex = 10 ' Green
Case Else
lngColourIndex = 15 ' Gray
End Select
rngDates.Offset(Target.Row - 1, lngStartCol - 1).Resize(1, lngEndCol - lngStartCol + 1).Interior.ColorIndex = lngColourIndex
End If
End If
ErrGantt:
Exit Sub
End Sub
vjboaz, it really help those trying to help you if you state what you have tried and why it did not work.
If you have a non standard layout that you mention such.
Or even better, as Richard mentioned, post an example.
Bookmarks