I am new to VBA and try to build my own automated Gantt chart in Excel. I get the run time 424 error, where object is required. This happens when I call a function in a module (all vars are publically defined constants, refering to specific row and column numbers):
Call Colour_Spans(Define_Time_Spans_2(i, Fixed_rows, Fixed_columns, Start_week, Start_year, End_week, End_year, OPT, Task_dependency))
The colouring function, which colours the bars, is:
Public Sub Colour_Spans(s As Range)
s.Interior.ColorIndex = 1
End Sub
Everything works if condition If IsEmpty(Cells(Fixed_rows + i, Task_dep)) Then
is executed in the code below, but I get the error when condiction ElseIf Not IsEmpty(Cells(Fixed_rows + i, Task_dep)) Then
is executed. What is listed below basically defines the bars, which are afterwards coloured. The additional condition intends to introduce dependency in the time spans between the tasks. If I take it away or just suppress it, everything works smooth.
I used a lot of time on my own, but cannot find the reason. Much of the second part of the code is identical to the working part. Any help would be highly appreciated.
I thus copy the whole code, where the issue is. Let me know if you need the rest. Thank you in advance!
Public Function Define_Time_Spans_2(i, Fixed_rows, Fixed_columns, week_start_column, year_start_column, week_end_column, year_end_column, duration_weeks, Task_dep)
Dim Cancel_start_week_flag As Boolean
Dim Cancel_start_year_flag As Boolean
time_span_start_year = Cells(Fixed_rows + i, year_start_column) 'Iso_year_no(Cells(fixed_rows + i, week_start_column))
Cells(Fixed_rows + i, week_start_column).NumberFormat = "General"
If IsEmpty(Cells(Fixed_rows + i, Task_dep)) Then
' DEFINE YEAR INPUT
If Cells(Fixed_rows + i, year_start_column) < 2015 Or Cells(Fixed_rows + i, year_start_column) > 2020 Then
check_start_year = MsgBox("The specified start year " & Cells(Fixed_rows + i, year_start_column) & " in row " & Fixed_rows + i & " is outside the range 2015-2020!" & vbCrLf & "Would you like to change it?", vbYesNo + vbQuestion, "Wrong input!")
Select Case check_start_year
Case 6
Do While Cells(Fixed_rows + i, year_start_column) < 2015 Or Cells(Fixed_rows + i, year_start_column) > 2020
Cells(Fixed_rows + i, year_start_column) = InputBox("Enter start year number")
Loop
Case 7
Cancel_start_year_flag = True
End Select
If Cancel_start_year_flag = True Then
MsgBox "The time bars have not been updated!", vbExclamation, "The code was terminated!"
End
End If
time_span_start_year = Cells(Fixed_rows + i, year_start_column)
End If
' DEFINE WEEK INPUT
If time_span_start_year = 2015 Then
If Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 53 Then
check_start_week = MsgBox("The specified start week " & Cells(Fixed_rows + i, week_start_column) & " in row " & Fixed_rows + i & " does not exist in year " & time_span_start_year & "!" & vbCrLf & "Would you like to change it?", vbYesNo + vbQuestion, "Wrong input!")
Select Case check_start_week
Case 6
Do While Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 53
Cells(Fixed_rows + i, week_start_column) = InputBox("Enter start week number")
Loop
Case 7
Cancel_start_week_flag = True
End Select
If Cancel_start_week_flag = True Then
MsgBox "The time bars have not been updated!", vbExclamation, "The code was terminated!"
End
End If
End If
time_span_start_week = Cells(Fixed_rows + i, week_start_column) ' Iso_week_no(Cells(fixed_rows + i, week_start_column))
ElseIf time_span_start_year = 2016 Then
If Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 52 Then
check_start_week = MsgBox("The specified start week " & Cells(Fixed_rows + i, week_start_column) & " in row " & Fixed_rows + i & " does not exist in year " & time_span_start_year & "!" & vbCrLf & "Would you like to change it?", vbYesNo + vbQuestion, "Wrong input!")
Select Case check_start_week
Case 6
Do While Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 52
Cells(Fixed_rows + i, week_start_column) = InputBox("Enter start week number")
Loop
Case 7
Cancel_start_week_flag = True
End Select
If Cancel_start_week_flag = True Then
MsgBox "The time bars have not been updated!", vbExclamation, "The code was terminated!"
End
End If
End If
time_span_start_week = Cells(Fixed_rows + i, week_start_column) + 53 'Iso_week_no(Cells(fixed_rows + i, week_start_column)) + 53 ' 2016
ElseIf time_span_start_year = 2017 Then
If Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 52 Then
check_start_week = MsgBox("The specified start week " & Cells(Fixed_rows + i, week_start_column) & " in row " & Fixed_rows + i & " does not exist in year " & time_span_start_year & "!" & vbCrLf & "Would you like to change it?", vbYesNo + vbQuestion, "Wrong input!")
Select Case check_start_week
Case 6
Do While Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 52
Cells(Fixed_rows + i, week_start_column) = InputBox("Enter start week number")
Loop
Case 7
Cancel_start_week_flag = True
End Select
If Cancel_start_week_flag = True Then
MsgBox "The time bars have not been updated!", vbExclamation, "The code was terminated!"
End
End If
End If
time_span_start_week = Cells(Fixed_rows + i, week_start_column) + 53 + 52 ' Iso_week_no(Cells(fixed_rows + i, week_start_column)) + 53 + 52 ' 2017
ElseIf time_span_start_year = 2018 Then
If Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 52 Then
check_start_week = MsgBox("The specified start week " & Cells(Fixed_rows + i, week_start_column) & " in row " & Fixed_rows + i & " does not exist in year " & time_span_start_year & "!" & vbCrLf & "Would you like to change it?", vbYesNo + vbQuestion, "Wrong input!")
Select Case check_start_week
Case 6
Do While Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 52
Cells(Fixed_rows + i, week_start_column) = InputBox("Enter start week number")
Loop
Case 7
Cancel_start_week_flag = True
End Select
If Cancel_start_week_flag = True Then
MsgBox "The time bars have not been updated!", vbExclamation, "The code was terminated!"
End
End If
End If
time_span_start_week = Cells(Fixed_rows + i, week_start_column) + 53 + 52 + 52 ' Iso_week_no(Cells(fixed_rows + i, week_start_column)) + 53 + 52 + 52 ' 2018
ElseIf time_span_start_year = 2019 Then
If Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 52 Then
check_start_week = MsgBox("The specified start week " & Cells(Fixed_rows + i, week_start_column) & " in row " & Fixed_rows + i & " does not exist in year " & time_span_start_year & "!" & vbCrLf & "Would you like to change it?", vbYesNo + vbQuestion, "Wrong input!")
Select Case check_start_week
Case 6
Do While Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 52
Cells(Fixed_rows + i, week_start_column) = InputBox("Enter start week number")
Loop
Case 7
Cancel_start_week_flag = True
End Select
If Cancel_start_week_flag = True Then
MsgBox "The time bars have not been updated!", vbExclamation, "The code was terminated!"
End
End If
End If
time_span_start_week = Cells(Fixed_rows + i, week_start_column) + 53 + 52 + 52 + 52 ' Iso_week_no(Cells(fixed_rows + i, week_start_column)) + 53 + 52 + 52 + 52 ' 2019
ElseIf time_span_start_year = 2020 Then
If Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 53 Then
check_start_week = MsgBox("The specified start week " & Cells(Fixed_rows + i, week_start_column) & " in row " & Fixed_rows + i & " does not exist in year " & time_span_start_year & "!" & vbCrLf & "Would you like to change it?", vbYesNo + vbQuestion, "Wrong input!")
Select Case check_start_week
Case 6
Do While Cells(Fixed_rows + i, week_start_column) < 1 Or Cells(Fixed_rows + i, week_start_column) > 53
Cells(Fixed_rows + i, week_start_column) = InputBox("Enter start week number")
Loop
Case 7
Cancel_start_week_flag = True
End Select
If Cancel_start_week_flag = True Then
MsgBox "The time bars have not been updated!", vbExclamation, "The code was terminated!"
End
End If
End If
time_span_start_week = Cells(Fixed_rows + i, week_start_column) + 53 + 52 + 52 + 52 + 52 ' Iso_week_no(Cells(fixed_rows + i, week_start_column)) + 53 + 52 + 52 + 52 + 52 ' 2020
End If
time_span_end_week = time_span_start_week + Cells(Fixed_rows + i, duration_weeks) - 1
Set Define_Time_Spans_2 = Range(Cells(Fixed_rows + i, Fixed_columns + time_span_start_week), Cells(Fixed_rows + i, Fixed_columns + time_span_end_week))
' OUTPUT
If time_span_end_week <= 53 Then
end_year_output = 2015
end_week_output = time_span_end_week
ElseIf time_span_end_week > 53 And time_span_end_week <= 53 + 52 Then
end_year_output = 2016
end_week_output = time_span_end_week - 53
ElseIf time_span_end_week > 53 + 52 And time_span_end_week <= 53 + 52 + 52 Then
end_year_output = 2017
end_week_output = time_span_end_week - (53 + 52)
ElseIf time_span_end_week > 53 + 52 + 52 And time_span_end_week <= 53 + 52 + 52 + 52 Then
end_year_output = 2018
end_week_output = time_span_end_week - (53 + 52 + 52)
ElseIf time_span_end_week > 53 + 52 + 52 + 52 And time_span_end_week <= 53 + 52 + 52 + 52 + 52 Then
end_year_output = 2019
end_week_output = time_span_end_week - (53 + 52 + 52 + 52)
ElseIf time_span_end_week > 53 + 52 + 52 + 52 + 52 And time_span_end_week <= 53 + 52 + 52 + 52 + 52 + 53 Then
end_year_output = 2020
end_week_output = time_span_end_week - (53 + 52 + 52 + 52 + 52)
End If
'THE PROBLEMATIC PART
ElseIf Not IsEmpty(Cells(Fixed_rows + i, Task_dep)) Then
ref_task = Cells(Fixed_rows + i, Task_dep) ' Reads the number of task to depend on => nums in B should be unique!
Dep_year = Cells(Fixed_rows + ref_task, year_end_column) ' Start_year
Dep_week = Cells(Fixed_rows + ref_task, week_end_column) ' Start_week
time_span_end_week = Dep_week + Cells(Fixed_rows + i, duration_weeks) '- 1
If Dep_year = 2015 Then
If time_span_end_week <= 53 Then
end_year_output = 2015
end_week_output = time_span_end_week
ElseIf time_span_end_week > 53 And time_span_end_week <= 53 + 52 Then
end_year_output = 2016
end_week_output = time_span_end_week - 53
ElseIf time_span_end_week > 53 + 52 And time_span_end_week <= 53 + 52 + 52 Then
end_year_output = 2017
end_week_output = time_span_end_week - (53 + 52)
ElseIf time_span_end_week > 53 + 52 + 52 And time_span_end_week <= 53 + 52 + 52 + 52 Then
end_year_output = 2018
end_week_output = time_span_end_week - (53 + 52 + 52)
ElseIf time_span_end_week > 53 + 52 + 52 + 52 And time_span_end_week <= 53 + 52 + 52 + 52 + 52 Then
end_year_output = 2019
end_week_output = time_span_end_week - (53 + 52 + 52 + 52)
ElseIf time_span_end_week > 53 + 52 + 52 + 52 + 52 And time_span_end_week <= 53 + 52 + 52 + 52 + 52 + 53 Then
end_year_output = 2020
end_week_output = time_span_end_week - (53 + 52 + 52 + 52 + 52)
End If
ElseIf Dep_year = 2016 Then
If time_span_end_week <= 52 Then
end_year_output = 2016
end_week_output = time_span_end_week
ElseIf time_span_end_week > 52 And time_span_end_week <= 52 + 52 Then
end_year_output = 2017
end_week_output = time_span_end_week - 52
ElseIf time_span_end_week > 52 + 52 And time_span_end_week <= 52 + 52 + 52 Then
end_year_output = 2018
end_week_output = time_span_end_week - (52 + 52)
ElseIf time_span_end_week > 52 + 52 + 52 And time_span_end_week <= 52 + 52 + 52 + 52 Then
end_year_output = 2019
end_week_output = time_span_end_week - (52 + 52 + 52)
ElseIf time_span_end_week > 52 + 52 + 52 + 52 And time_span_end_week <= 52 + 52 + 52 + 52 + 53 Then
end_year_output = 2020
end_week_output = time_span_end_week - (52 + 52 + 52 + 52)
End If
ElseIf Dep_year = 2017 Then
If time_span_end_week <= 52 Then
end_year_output = 2017
end_week_output = time_span_end_week
ElseIf time_span_end_week > 52 And time_span_end_week <= 52 + 52 Then
end_year_output = 2018
end_week_output = time_span_end_week - 52
ElseIf time_span_end_week > 52 + 52 And time_span_end_week <= 52 + 52 + 52 Then
end_year_output = 2019
end_week_output = time_span_end_week - (52 + 52)
ElseIf time_span_end_week > 52 + 52 + 52 And time_span_end_week <= 52 + 52 + 52 + 53 Then
end_year_output = 2020
end_week_output = time_span_end_week - (52 + 52 + 52)
End If
ElseIf Dep_year = 2018 Then
If time_span_end_week <= 52 Then
end_year_output = 2018
end_week_output = time_span_end_week
ElseIf time_span_end_week > 52 And time_span_end_week <= 52 + 52 Then
end_year_output = 2019
end_week_output = time_span_end_week - 52
ElseIf time_span_end_week > 52 + 52 And time_span_end_week <= 52 + 52 + 53 Then
end_year_output = 2020
end_week_output = time_span_end_week - (52 + 52)
End If
ElseIf Dep_year = 2019 Then
If time_span_end_week <= 52 Then
end_year_output = 2019
end_week_output = time_span_end_week
ElseIf time_span_end_week > 52 And time_span_end_week <= 52 + 53 Then
end_year_output = 2020
end_week_output = time_span_end_week - 52
End If
ElseIf Dep_year = 2020 Then
end_year_output = 2020
end_week_output = time_span_end_week
End If
End If
Cells(Fixed_rows + i, week_end_column).NumberFormat = "General"
Cells(Fixed_rows + i, week_end_column) = end_week_output
Cells(Fixed_rows + i, year_end_column) = end_year_output
End Function