Results 1 to 1 of 1

Thread: [PROJECT] Color row after field changed - doesn't work for subprojects

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Location
    Portugal
    Posts
    1

    Question [PROJECT] Color row after field changed - doesn't work for subprojects

    I'm trying to set up VBA code to color the whole row when the field Text12 is equal to "OK" or "NOK" (and other keywords). The code below works at a Master Project level, that is, because it uses the Project_Change event. However, the event doesn't trigger if I edit a task that is in a SubProject. I'm using the App_ProjectBeforeTaskChange event to detect when a task is changed > check if its the Text12 field > set a bool to true so it checks on the Project_Change event and color the row.

    If I try to run the code directly from App_ProjectBeforeTaskChange, VBA throws the 1100 error "this method is not available in this situation". This happens at the SelectRow line and at the Font32Ex CellColor line.

    I've tried using timers and DoEvents loops, but no avail. I don't know what else to try. It seems there's no threading either, so I can't color the rows after some miliseconds.

    You can create an empty project and copy the code below and it should work for you, if you want to help me I'm not a VBA expert btw, started learning two months ago.

    ThisProject:
    Code:
    Private Sub Project_Open(ByVal pj As Project)
        InitializeEventHandler 'this runs at start up. You could also use a button to call this everytime you change the code, so you don't need to restart Project
    End Sub
    Module1: Regular Module
    Code:
    Option Explicit
    Dim EventHandler As EventClassModule
    
    Sub InitializeEventHandler()
        ' Initializing the object to handle the events
        Set EventHandler = New EventClassModule
        Set EventHandler.App = Application
        Set EventHandler.proj = Application.ActiveProject
    End Sub
    
    Sub ApplyColor()
        ' this is the sub that changed the color, from the Project_Change event
        Dim t As Task
        
        Set t = EventHandler.ChangedTask
        
        If Not t Is Nothing Then
            Find "Unique ID", "equals", t.UniqueID
            SelectRow
            Select Case EventHandler.NewValue
                Case "OK"
                    Font32Ex CellColor:=14282722 'green
                Case "NOK"
                    Font32Ex CellColor:=11324407 'red
                Case "PROGRESS"
                    Font32Ex CellColor:=65535 'blue
                Case "REPEAT"
                    Font32Ex CellColor:=15652797 'yellow
                Case Else
                    Font32Ex CellColor:=-16777216 'no color
            End Select
        End If
    End Sub
    EventClassModule: ClassModule
    Code:
    Public WithEvents App As Application
    Public WithEvents proj As Project
    
    Public NewValue As String 'used to check what the user typed in the Text12 field
    Public ChangePending As Boolean 'switch bool to trigger the ApplyColor
    Public ChangedTask As Task 'reference to the changed task, to select its row later in ApplyColor
    
    Private Sub App_ProjectBeforeTaskChange(ByVal tsk As Task, ByVal Field As PjField, ByVal NewVal As Variant, Cancel As Boolean)
        ' this runs when changing a task
        If Field = 188743998 Then 'Custom field Text12
            Set ChangedTask = tsk
            NewValue = NewVal
            ChangePending = True
        End If
    End Sub
    
    Private Sub Proj_Change(ByVal pj As Project)
        ' this runs right after changing a task
        If ChangePending Then
            ApplyColor
            ChangePending = False
        End If
    End Sub
    Last edited by Klausbdl; Feb 18th, 2025 at 06:24 AM.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width