Consulting

Results 1 to 4 of 4

Thread: Select Case only running one sub scenario

  1. #1
    VBAX Newbie
    Joined
    Sep 2015
    Posts
    2
    Location

    Select Case only running one sub scenario

    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
    Last edited by SamT; 09-11-2015 at 10:05 PM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Newbie
    Joined
    Sep 2015
    Posts
    2
    Location
    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
    Last edited by SamT; 09-14-2015 at 05:37 PM. Reason: Used the # icon to put copde tags around the sub

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •