My code gives the error stated in the title and I don't know why, it happens in this line:
Time = KNP.Cells(2, LastVans) - KNP.Cells(2, FirstVans)
Everything I could but nothing seemed to work...
Sub CalculateKPI()
Dim RKPI, KNP, ST As Worksheet
Set RKPI = Sheets("Results KPI")
Set KNP = Sheets("SimNodes")
Set ST = Sheets("SimStartTimes")
Dim LastVans As Long
Dim FirstVans As Long
Dim Time As Long
RKPI.Cells(1, 2) = "Maximum Crowdedness"
RKPI.Cells(1, 3) = "Occupation Time"
'### Einde Stap 2
For i = 1 To 25
RKPI.Cells(i + 1, 1) = "Stage" & i
lastcol = Split(KNP.Cells(i + 2,
Columns.Count).End(xlToLeft).Address, "$")(1)
TotalMaximumCrowdedness(i) = TotalMaximumCrowdedness(i) +
Application.WorksheetFunction.Max(KNP.Range("B" & i + 2 & ":" & lastcol
& i + 2))
'## KPI occupationtime per stage
lastcolnr = KNP.Cells(i + 2, Columns.Count).End(xlToLeft).Column
FirstVans = 0
LastVans = 0
Dim g As Long
For g = 1 To 24
For p = 2 To lastcolnr
If KNP.Cells(g + 2, p) <> 0 Then
FirstVans = p
Exit For
End If
Next
Next g
For p = lastcolnr To 2 Step -1
If KNP.Cells(, p) <> 0 Then
LastVans = p
Exit For
End If
Next
Time = KNP.Cells(2, LastVans) - KNP.Cells(2, FirstVans)
TotalMaximumCrowdedness(i) = TotalMaximumCrowdedness(i) + Time
lastcolnr = KNP.Cells(i + 2, Columns.Count).End(xlToLeft).Column
Next
Call Costs
Call KPI3
Call BusinessStageChange
End Sub
Any help would be appreciated, I am pretty new to VBA. If anyone needs the file to help out, I would be pleased to supply it.
LastVansorFirstVansis not running. Learning to use the debugger is one of the most powerful additions a programmer can make to their toolbox - it's never too soon to start to use it.If KNP.Cells(, p) <> 0 Then- is that intended asCells(1, p)? Might also want to reset FirstVans/LastVans inside eachgloop iteration, unless you expect values to "carry over" between iterations.