PDA

View Full Version : Looping thorugh all tasks fails after task deleted



alex_hill
06-01-2016, 11:50 PM
I am trying to loop through all tasks, check them against a condition and then delete the ones that match the condition. The problem I am having is that the foreach loop exists after I delete the first task. In the code below I am trying to identify whether the task matches one in a variant array (imported from excel)




For Each Task In ActiveProject.Tasks

If Task.Text10 = "Work Order" Then
FoundWO = False
For j = LBound(vData) To UBound(vData)
If vData(j, 2) = Task.Text3 Then 'Found the work order header, dont delete it
FoundWO = True
Exit For
End If
Next j
If FoundWO = False Then
Task.Delete
End If
End If
Next Task


The loop will work fine UNTIL I delete the first task. After the delete it exits at the "Next Task" line.

Can anyone help me understand why this is happening?

mdmackillop
06-04-2016, 10:07 AM
Try working backwards

For j = UBound(vData) To LBound(vData) step -1

SamT
06-04-2016, 09:46 PM
I am trying to loop through all tasks, check them against a condition and then delete the ones that match the condition.

For Each Task In ActiveProject.Tasks

If Task.Text10 = "Work Order" Then
For j = UBound(vData) to LBound(vData) Step -1
If vData(j, 2) = Task.Text3 Then 'matches condition
Task.Delete
End If
'Alternate Condition:
'vData(j, 2) <> Task.Text3
Next j
End If
Next Task