Code executing but not doing anything

S

scott_hanebutt

I have written vb code to color code some tasks. When I step through the
code it seems to execute correctly, however the color does not change.

To elaborate:
I do not recieve any unexpected errors.

The line that sets the color is executed (verified by stepping through the
code) but the color does not change.

I coppied the same code outside of the Select case and it does change color.

Sub S2_2005()

' Filters tasks so only ones with a date in "Finish1" appear
FilterEdit Name:="S2_2005", taskfilter:=True, create:=True,
overwriteexisting:=True, FieldName:="Finish1", _
Test:="does not equal", Value:="NA", ShowInMenu:=False,
showsummarytasks:=True
FilterApply Name:="S2_2005"

On Error GoTo ErrHandler
For Each Task In ActiveProject.Tasks
Select Case Task.Number19
Case Is > 0
SelectRow Row:=Task.ID, rowrelative:=False
Font Color:=pjGreen
Case Is < 0
SelectRow Row:=Task.ID, rowrelative:=False
Font Color:=pjRed
Case Else
End Select

'SelectRow Row:=Task.ID, rowrelative:=False
'Font Color:=pjRed
Next Task

ErrHandler:
Resume Next

End Sub

Any thoughts would be apreciated.

Scott Hanebutt
 
J

John

scott_hanebutt said:
I have written vb code to color code some tasks. When I step through the
code it seems to execute correctly, however the color does not change.

To elaborate:
I do not recieve any unexpected errors.

The line that sets the color is executed (verified by stepping through the
code) but the color does not change.

I coppied the same code outside of the Select case and it does change color.

Sub S2_2005()

' Filters tasks so only ones with a date in "Finish1" appear
FilterEdit Name:="S2_2005", taskfilter:=True, create:=True,
overwriteexisting:=True, FieldName:="Finish1", _
Test:="does not equal", Value:="NA", ShowInMenu:=False,
showsummarytasks:=True
FilterApply Name:="S2_2005"

On Error GoTo ErrHandler
For Each Task In ActiveProject.Tasks
Select Case Task.Number19
Case Is > 0
SelectRow Row:=Task.ID, rowrelative:=False
Font Color:=pjGreen
Case Is < 0
SelectRow Row:=Task.ID, rowrelative:=False
Font Color:=pjRed
Case Else
End Select

'SelectRow Row:=Task.ID, rowrelative:=False
'Font Color:=pjRed
Next Task

ErrHandler:
Resume Next

End Sub

Any thoughts would be apreciated.

Scott Hanebutt

Scott,
First of all, the "On Error GoTo ErrHandler" is doing you in. It
effectively locks out any error message you might receive so even though
it may look like your code is executing without an error, it is not.
Comment out the "On Error..." line and see what happens.

The next problem with your code is that the filter isn't doing what you
think. I assume you want to apply the filter and then loop through only
those tasks shown by the filter, but your code isn't doing that. The
"For Each Task in ActivProject.Tasks" will loop through ALL tasks in the
project. However, the "SelectRow" Method operates on the tasks in the
active view, which of course is the filtered set. So basically the code
is "fighting with itself" but it's not going to tell you it is unhappy
because of the suppressed error handling.

Try the below mods and see if it works better. Note: I didn't have time
to actually try your code with the mods so it may still have a glitch,
but at least it should help point you in the right direction.

[apply your filter]
SelectTaskColumn
For Each t In ActiveSelection.Tasks
[your case statements}
Next t

End Sub

Hope this helps.
John
Project MVP
 
S

scott_hanebutt

Thanks for the help. However, my problem is still not solved.

My errorhandler is necessary because some of the time Task.Number19 is not a
valid number. I know that the line of code that sets the color is executed
without an error because I steped through the code and it did not go to the
ErrHandler.

I have discovered that the problem is caused by selecting the wrong row. My
code makes the active row where Row:=Task.ID. This is a row number that I
don't even have text on.

I need to know how to select the row that has the Task.ID

Thanks,
Scott Hanebutt

John said:
scott_hanebutt said:
I have written vb code to color code some tasks. When I step through the
code it seems to execute correctly, however the color does not change.

To elaborate:
I do not recieve any unexpected errors.

The line that sets the color is executed (verified by stepping through the
code) but the color does not change.

I coppied the same code outside of the Select case and it does change color.

Sub S2_2005()

' Filters tasks so only ones with a date in "Finish1" appear
FilterEdit Name:="S2_2005", taskfilter:=True, create:=True,
overwriteexisting:=True, FieldName:="Finish1", _
Test:="does not equal", Value:="NA", ShowInMenu:=False,
showsummarytasks:=True
FilterApply Name:="S2_2005"

On Error GoTo ErrHandler
For Each Task In ActiveProject.Tasks
Select Case Task.Number19
Case Is > 0
SelectRow Row:=Task.ID, rowrelative:=False
Font Color:=pjGreen
Case Is < 0
SelectRow Row:=Task.ID, rowrelative:=False
Font Color:=pjRed
Case Else
End Select

'SelectRow Row:=Task.ID, rowrelative:=False
'Font Color:=pjRed
Next Task

ErrHandler:
Resume Next

End Sub

Any thoughts would be apreciated.

Scott Hanebutt

Scott,
First of all, the "On Error GoTo ErrHandler" is doing you in. It
effectively locks out any error message you might receive so even though
it may look like your code is executing without an error, it is not.
Comment out the "On Error..." line and see what happens.

The next problem with your code is that the filter isn't doing what you
think. I assume you want to apply the filter and then loop through only
those tasks shown by the filter, but your code isn't doing that. The
"For Each Task in ActivProject.Tasks" will loop through ALL tasks in the
project. However, the "SelectRow" Method operates on the tasks in the
active view, which of course is the filtered set. So basically the code
is "fighting with itself" but it's not going to tell you it is unhappy
because of the suppressed error handling.

Try the below mods and see if it works better. Note: I didn't have time
to actually try your code with the mods so it may still have a glitch,
but at least it should help point you in the right direction.

[apply your filter]
SelectTaskColumn
For Each t In ActiveSelection.Tasks
[your case statements}
Next t

End Sub

Hope this helps.
John
Project MVP
 
J

John

scott_hanebutt said:
Thanks for the help. However, my problem is still not solved.

My errorhandler is necessary because some of the time Task.Number19 is not a
valid number. I know that the line of code that sets the color is executed
without an error because I steped through the code and it did not go to the
ErrHandler.

I have discovered that the problem is caused by selecting the wrong row. My
code makes the active row where Row:=Task.ID. This is a row number that I
don't even have text on.

I need to know how to select the row that has the Task.ID

Thanks,
Scott Hanebutt


Scott,
Ok, I understand your issue with Number19 not always being valid. There
may or may not be better ways to work around that.

As I mentioned I didn't have time to check out your code with my
suggested mods yesterday but today, I had a little more time. The
following code structure should do what you need:
[apply your filter]
SelectTaskColumn
n = 0
For Each t In ActiveSelection.Tasks
If t.Number19 > 0 Then
SelectRow row:=n, rowrelative:=False
Font Color:=pjGreen
Elseif t.Number19 < 0 Then
SelectRow row:=n, rowrelative:=False
Font Color:=pjRed
End If
n = n + 1
Next t

If it still doesn't do what you want, tell me more and we'll find a
solution.

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