VB Code to delete tasks based on values in custom field - not dele

G

GA

We have written the following code to delete tasks that have a value of "yes"
in Flag1 (Project Pro 2003)

Dim T As Task

For Each T In ActiveProject.Tasks
If Not (T Is Nothing) Then
If T.Flag1 = True Then
T.Delete
End If
End If
Next T

However, when the macro is executed, nothing is deleted. Now this sub
routine is being called in a Master project where the links to the
subprojects have been broken in a previous subroutine. It appears to be
ignoring the value in the Flag1 field. In stepping through the macro it
shows each line as being False. Even when the value is "Yes".
 
J

Jack Dahlgren

Maybe try:

If T.Flag1 Then
....

I don't have 2003 available to troubleshoot, but this is the first thing I'd
try. After that, I'd look at t.flag1 = "Yes" or something similar.

-Jack
 
J

Jan De Messemaeker

Hi,

Sorry but the activeproject is your master project.
This code doesn't look into the subprojects!

This is better:
for each Subpr in activeproject.subprojects
set SmallPr=subpr.sourceproject
for each T in Smallpr.tasks

etcetera

An alternative is
viewapply "Gantt Chart"
filterapply "All Tasks"
outlineshowalltasks
for each T in activeselection.tasks

Hope this helps,
 
J

John

GA said:
We have written the following code to delete tasks that have a value of "yes"
in Flag1 (Project Pro 2003)

Dim T As Task

For Each T In ActiveProject.Tasks
If Not (T Is Nothing) Then
If T.Flag1 = True Then
T.Delete
End If
End If
Next T

However, when the macro is executed, nothing is deleted. Now this sub
routine is being called in a Master project where the links to the
subprojects have been broken in a previous subroutine. It appears to be
ignoring the value in the Flag1 field. In stepping through the macro it
shows each line as being False. Even when the value is "Yes".

GA,
If indeed you have unlinked the subprojects from the master, there is
nothing wrong with your code. However, the fact that your code is not
working tells me that, (as Jan suggested), your subprojects may not have
been unlinked from the master. The easiest way to tell is to look at the
ID numbers. If they are continuous, then the subprojects are unlinked
and you do have a static master. However, if the IDs repeat for each
subproject, then you need to re-visit your code that supposedly unlinked
them.

Let's assume you do have a static master. Since your code does work, (I
tested it), something else must be wrong. The code you have written does
background processing so the active view is irrelevant. The only other
thing I can think of is that the active project is not the master
project you want (i.e. another project is actually active when you run
the macro).

By the way, are you sure the flag field you set is Task Flag1? If
instead you set flag1 in a resource view or one of the combination views
(i.e. Resource Usage or Task Usage), then you probably set Resource
Flag1 or Assignment Flag1. They are all three separate independent flag
fields.

Let us know what you find.

John
Project MVP
 
G

GA

Thanks, but as stated previously the link to subprojects has been broken, ID
numbers are continuous and the project is stand alone after an unlink
sub-routine runs just prior to this delete sub-routine. So there are no
subprojects for the code to look into.
 
G

GA

John, thank you for your thorough input. The ID numbers are continuous so
the project is static. The Flag1 is a Task Flag1. The odd thing is if I
manually change a value I have to change it a couple of times and then it
will get picked up when I run the sub-routine. now I have the same code for
another Task Flag, Flag7. Funny thing there is that when I excute the
sub-routine once, it doesn't work...just like the Flag1 sub-routine. But
when I run it a second time, it works and deletes the tasks with "Yes" in
Flag7. Basically, I have 2 different sub-routines deleting tasks, one based
on Task Flag1 = "Yes" and the other based on Task Flag7 = "Yes". Flag7
works, on the second attempt. Flag1 doesn't work at all. now if I enter a
new row to the project, it works fine. There is something with the way those
entries are sitting in those fields that the code just doesn't like.
 
G

GA

Thank you. Setting T.Flag1 = "Yes" ends up deleting all the tasks in my
project.

Thanks
 
J

Jan De Messemaeker

Wait.. a vague memory pops up... seen this years ago.
Have you installed any Service packs?
 
G

GA

I would have to send in a ticket to our dev group to find out. On the Help |
About screen it shows Project Pro 2003 and then a series of numbers ending in
1801.15) SP2.

I'm not sure where to look if that is the most up to date version of Project
Pro 2003.
 
J

Jan De Messemaeker

OK sorry, the problem I remember was definitely before SP2.
Forget my hunch.
 
J

John

GA said:
John, thank you for your thorough input. The ID numbers are continuous so
the project is static. The Flag1 is a Task Flag1. The odd thing is if I
manually change a value I have to change it a couple of times and then it
will get picked up when I run the sub-routine. now I have the same code for
another Task Flag, Flag7. Funny thing there is that when I excute the
sub-routine once, it doesn't work...just like the Flag1 sub-routine. But
when I run it a second time, it works and deletes the tasks with "Yes" in
Flag7. Basically, I have 2 different sub-routines deleting tasks, one based
on Task Flag1 = "Yes" and the other based on Task Flag7 = "Yes". Flag7
works, on the second attempt. Flag1 doesn't work at all. now if I enter a
new row to the project, it works fine. There is something with the way those
entries are sitting in those fields that the code just doesn't like.

GA,
OK, let's look at a couple of other things. Are you running the macro
from your Global or is it attached to the file itself? Are you running
Project 2003 with Project Server?

You say you have to run the macro a second time before it deletes the
tasks. What evidence do you have that the code is in fact executed the
first time? Note: you could modify your code as follows and then look in
the Immediate Window of the VB Editor after the first attempt at
execution.

For Each t in ActiveProject.Tasks
If Not t is Nothing Then
If t.Flag1 = True Then
t.Delete
End If
End if
Next t
Debug.Print "Run complete"

If the statement appears, the code did execute. However, be advised that
the statement will be written each time the macro is run - you run the
macro two times, the statement will appear two times.

If you are running the macro from your Global, and I highly recommend
doing it that way, but it still doesn't seem to work properly, you may
have a corrupt file. Go to our MVP website at,
http://www.mvps.org/project/faqs.htm, and take a look at FAQ 43.

Let us know

John
Project MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top