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 meI'm not a VBA expert btw, started learning two months ago.
ThisProject:
Module1: Regular ModuleCode: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
EventClassModule: ClassModuleCode: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
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




I'm not a VBA expert btw, started learning two months ago.
Reply With Quote
