问题
I'm trying to modify a formula in a custom field in VBA. I currently have the following code:
CustomFieldSetFormula FieldID:=pjCustomTaskNumber9
Formula = IIf(IIf([% Complete] = 100, 2, IIf([% Complete] < 100 And [Finish] > [Baseline Finish], 1, IIf([Unique ID] > maxUID, 3))))
CustomFieldProperties FieldID:=pjCustomTaskNumber9, Attribute:=pjFieldAttributeFormula, SummaryCalc:=pjCalcFormula, GraphicalIndicators:=True, AutomaticallyRolldownToAssn:=False
The maxUID
will change most likely each time I plan to use the macro. When attempting to execute the code, I get the error message:
External name not defined" for the "[% Complete]
Updated Code: Getting error on 2nd line of code still.
customFormula = "IIf(IIf([% Complete] = 100, 2, IIf([% Complete] < 100 And [Finish] > [Baseline Finish], 1, IIf([Unique ID] > " & maxUID & ", 3))))"
CustomFieldSetFormula FieldID:=pjCustomTaskNumber9, Formula:=customFormula
CustomFieldProperties FieldID:=pjCustomTaskNumber9, Attribute:=pjFieldAttributeFormula, SummaryCalc:=pjCalcFormula, GraphicalIndicators:=True
回答1:
Build the custom formula as a string and include it when calling the CustomFieldSetFormula
method:
Dim customFormula As String
customFormula = "IIf([% Complete] = 100, 2, IIf([% Complete] < 100 And [Finish] > [Baseline Finish], 1, IIf([Unique ID] > " & maxUID & ", 3)))"
CustomFieldSetFormula FieldID:=pjCustomTaskNumber9, Formula:=customFormula
CustomFieldProperties FieldID:=pjCustomTaskNumber9, Attribute:=pjFieldAttributeFormula, SummaryCalc:=pjCalcFormula, GraphicalIndicators:=True, AutomaticallyRolldownToAssn:=False
来源:https://stackoverflow.com/questions/48752841/how-to-customize-custom-field-formula-in-vba