【问题标题】:How to Customize Custom Field Formula in VBA?如何在 VBA 中自定义自定义字段公式?
【发布时间】:2020-07-01 07:14:05
【问题描述】:

我正在尝试修改 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 很可能会发生变化。尝试执行代码时,我收到错误消息:

“[% Complete] 的外部名称未定义”

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

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

【问题讨论】:

    标签: vba ms-project


    【解决方案1】:

    将自定义公式构建为字符串,并在调用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
    

    【讨论】:

    • 感谢您的评论!我使用了您提供的代码,但在第 3 行 (CustomFieldSetFormula) 出现错误,以黄色突出显示。
    • 尝试删除 AutomaticallyRolldownToAssn 部分。
    • 不幸的是仍然没有为我工作。请看一下我更新的代码,我编辑了最初的问题,所以我可以向你展示我现在拥有的东西,也许我在某个地方犯了一个基本错误。
    • 公式有一个无关的前导 IIf。删除它和结尾的右括号之一,它现在应该可以工作了。
    猜你喜欢
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多