I create a VB program to automatically update a Gantt chart for a group project. But now the team wants to add a new colum. The problem is that adding a new column will change my code and make it unusable. Rows can be added without changing the code, but I would have to update all my code if new colums are added. How can I add a column without chaning my VB code?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim StartDate_Row As Integer
Dim Total_Weeks As Integer
Dim Date_Week_Column As Integer
Dim Number_of_Weeks As Integer
Dim Date_Week_Column_Color As Integer
StartDate_Row = 10
Date_Week_Column = 8
Range("H9:AN25").Interior.Color = xlNone
Do
For Total_Weeks = 1 To 33
If Cells(StartDate_Row, 5).Value = Cells(8, Date_Week_Column).Value Then
Date_Week_Column_Color = Date_Week_Column
For Number_of_Weeks = 1 To Cells(StartDate_Row, 6).Value
If Cells(StartDate_Row, 7).Value = 25 Then
Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(204, 255, 299)
End If
If Cells(StartDate_Row, 7).Value = 50 Then
Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(153, 255, 204)
End If
If Cells(StartDate_Row, 7).Value = 75 Then
Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(102, 255, 178)
End If
If Cells(StartDate_Row, 7).Value = 100 Then
Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(50, 200, 100)
End If
If Cells(StartDate_Row, 7).Value = 0 Then
Cells(StartDate_Row, Date_Week_Column_Color).Interior.Color = RGB(149, 179, 215)
End If
Date_Week_Column_Color = Date_Week_Column_Color + 1
Next Number_of_Weeks
End If
Date_Week_Column = Date_Week_Column + 1
Next Total_Weeks
Date_Week_Column = 8
StartDate_Row = StartDate_Row + 1
Loop While (Not IsEmpty(Cells(StartDate_Row, 5)))
End Sub
Date_Week_Column = 8to match the right column after inserting some extra columns. AlsoIf Cells(StartDate_Row, 7)the7thcolumn is hardcoded currently - it may need to be adjusted as well.