如何在VBA中自定义自定义字段公式?

时间:2018-02-12 18:10:47

标签: microsoft-project-vba

我试图修改VBA中自定义字段中的公式。我目前有以下代码:

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

每次我打算使用宏时,maxUID最有可能发生变化。尝试执行代码时,收到错误消息:

  

未定义外部名称&#34;对于&#34; [%完成]

更新的代码:第二行代码仍然出错。

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 个答案:

答案 0 :(得分:3)

将自定义公式构建为字符串,并在调用CustomFieldSetFormula方法时包含它:

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