PDA

View Full Version : Select Case only running one sub scenario



shim
09-11-2015, 04:16 PM
I'm very very new to VBA and don't have a need to use it a lot, so when I do I generally find a code that is similar to what I am looking for and try to tweak it. I am adding code in to a Microsoft Project 2013 document to conditionally format the color of the Start Date column if it is less than 100% complete. So far, the below code works up until the lines I've highlighted in red. The code seems to work accurately except that when there is a "TBD" in the data field it is sometimes turned green and sometimes skipped. I would like to have TBD in grey or even have it skipped over and be in Auto color/black. Any help is appreciated!





Sub ColorFormatDateCurrent()
Dim t As Task
Dim i As Integer

i = 1
For Each t In ActiveProject.Tasks
SelectTaskField Row:=i, RowRelative:=False, Column:="Start"

Select Case t.PercentComplete
Case Is < 100
Select Case t.Start
Case Is < ActiveProject.CurrentDate
Font Color:=pjGreen
Case "TBD"
Font Color:=pjGrey
Case Else
Font Color:=pjBlack

End Select
End Select

i = i + 1
Next t
End Sub

SamT
09-11-2015, 10:13 PM
You cannot nest Selects like that, but it looks like the outer select can be replaced with an IF


Sub ColorFormatDateCurrent()
Dim t As Task
Dim i As Integer

'This just doesn't look right???
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''
i = 1
For Each t In ActiveProject.Tasks
'Shouldn't you go through the Task Rows after you determine the task?
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
SelectTaskField Row:=i, RowRelative:=False, Column:="Start"

If t.PercentComplete < 100 Then
Select Case t.Start
Case Is < ActiveProject.CurrentDate
t.Font.Color:=pjGreen 'I am assuming a lot here.
Case "TBD"
t.Font.Color:=pjGrey
Case Else
t.Font.Color:=pjBlack

End Select
End If
i = i + 1
Next t
End Sub

shim
09-14-2015, 11:24 AM
Thank you. I tried the If adjustment suggested (though I had to change it back to "Font Color" as "t.Font.Color" was not accepted). It ran but still ignored the Case "TBD" and Case Else scenarios. :( Apologies for my naivety - I really don't understand the set up part at the top you are questioning enough to know if that is part of the problem. I did try moving or deleting portions but it wouldn't run then.




Sub ColorFormatDateCurrent()
Dim t As Task
Dim i As Integer
i = 1
For Each t In ActiveProject.Tasks
SelectTaskField Row:=i, RowRelative:=False, Column:="Start"

If t.PercentComplete < 100 Then
Select Case t.Start
Case Is < ActiveProject.CurrentDate
Font Color:=pjGreen
Case "TBD"
Font Color:=pjGrey
Case Else
Font Color:=pjBlack

End Select
End If
i = i + 1
Next t
End Sub

SamT
09-14-2015, 05:51 PM
BTW, the # icon inserts code tags, which format your code. You can select the code and click the #, or you can click the # then paste the code between the tags.

I don't know enough about MS Project to recognize the problem form the code, but What is "Current Date?"

You might try an If Then ElsIf since there are only three options

If t.PercentComplete < 100 Then
If t.Start < ActiveProject.CurrentDate Then
Font Color:=pjGreen
ElseIf t.Start = "TBD" Then
Font Color:=pjGrey
Else
Font Color:=pjBlack
End If
End If