在vba中分解R1C1公式

时间:2018-01-14 16:59:51

标签: vba

我正在制作一个需要多个&#34的R1C1公式;如果"条件。为了便于阅读和调试,我想将其分解为不同的行。我正在使用" &安培; _在第一行,然后使用"开始下一行。但是,我认为我遗漏了一些东西,因为我收到编译错误,说预期:列表分隔符或)。以下是我正在处理的代码。对此有任何帮助将非常感激。

Selection.FormulaR1C1 = IFERROR(" & _
"IF(RC5>=0.75," & _
"IF(AND(RC16=0,RC20=0),""No Action Req: None of the Predictors have an Estimate""," & _
"IF(AND(RC16=0,RC20<>0,RC8>RC9),""Less Similar Comp Retained: Check if P1 is important, no action req. otherwise""," & _
"IF(AND(RC16<>0,RC20=0,RC9>RC8),""Less Similar Comp Retained: Check if P2 is important, no action req. otherwise""," & _
"IF(AND(RC16=0,RC20<>0,RC8<RC9),""No Action Req: Model is retaining more similar comp.""," & _
"IF(AND(RC16<>0,RC20=0,RC9<RC8),""No Action Req: Model is retaining more similar comp.""," & _
"IF(AND(RC16<>0,RC20<>0,RC8>=70%,RC9>=70%),""Keep Both: Both similar comps""," & _
"IF(AND(RC16<>0,RC20<>0)," & _
"if(abs(RC8-RC9)<=5," & _
    "if((RC8/RC6)<(RC9/RC7),""Drop P1: P1 wins over P2 wrt score and count"",""Drop P2: P2 wins over P1 wrt score and count"")," & _
"IF(AND(RC16<>0,RC20<>0,RC8<RC9),""Drop P1: P2 is more similar to TGT""," & _
"IF(AND(RC16<>0,RC20<>0,RC9<RC8),""Drop P2: P1 is more similar to TGT""," & _
"""""))))))))))),"""")

1 个答案:

答案 0 :(得分:0)

你在这里错过了双引号:

Selection.FormulaR1C1 = IFERROR(" & _

应该是(注意开场=):

Selection.FormulaR1C1 = "=IFERROR(" & _

然后,公式中的每个字符串文字都需要对其双引号进行转义(即加倍)。

此代码编译并正确设置公式:

Selection.FormulaR1C1 = "=IFERROR(" & _
"IF(RC5>=0.75," & _
"IF(AND(RC16=0,RC20=0),""No Action Req: None of the Predictors have an Estimate""," & _
"IF(AND(RC16=0,RC20<>0,RC8>RC9),""Less Similar Comp Retained: Check if P1 is important, no action req. otherwise""," & _
"IF(AND(RC16<>0,RC20=0,RC9>RC8),""Less Similar Comp Retained: Check if P2 is important, no action req. otherwise""," & _
"IF(AND(RC16=0,RC20<>0,RC8<RC9),""No Action Req: Model is retaining more similar comp.""," & _
"IF(AND(RC16<>0,RC20=0,RC9<RC8),""No Action Req: Model is retaining more similar comp.""," & _
"IF(AND(RC16<>0,RC20<>0,RC8>=70%,RC9>=70%),""Keep Both: Both similar comps""," & _
"IF(AND(RC16<>0,RC20<>0)," & _
"if(abs(RC8-RC9)<=5," & _
    "if((RC8/RC6)<(RC9/RC7),""Drop P1: P1 wins over P2 wrt score and count"",""Drop P2: P2 wins over P1 wrt score and count"")," & _
"IF(AND(RC16<>0,RC20<>0,RC8<RC9),""Drop P1: P2 is more similar to TGT""," & _
"IF(AND(RC16<>0,RC20<>0,RC9<RC8),""Drop P2: P1 is more similar to TGT""," & _
"""""))))))))))),"""")"

但是我没有样本数据,公式对我来说评估为FALSE,这意味着嵌套条件逻辑本身可能有问题 - 说实话我不想调试这种公式。

反对Selection是危险的。认真考虑针对特定的Range对象,如果这是你的意思 - 这是一个简单的例子(可能或可能不是你需要的):

Dim targetCell As Range
Set targetCell = Sheet1.Range("A1")

targetCell.FormulaR1C1 = ...

IMO的真正解决方案是放弃使用公式评估该单元格的想法,而是在实际的VBA代码中实现条件逻辑。您需要Set Range RC5对象引用,代表RC16Dim result As String If WhateverRC5is.Value >= 0.75 Then If WhateverRC16Is.Value = 0 And WhateverRC20is.Value = 0 Then result = "No Action Req: None of the Predictors have an Estimate" ElseIf WhateverRC16Is.Value = 0 And WhateverRC20is.Value <> 0 And WhateverRC8is.Value > WhateverRC9is.Value Then result = "Less similar comp retained: check if P1 is important.." ElseIf ... ... End If Else '... End If targetCell.Value = result 以及您正在使用的每个其他单元格(为其指定有意义的名称)。结果可能如下所示:

 def congru(a,b,c):
    for i in range(0,c):
       if ((a*i - b)%c)== 0 :
          print(i)

使用适当的缩进和编译时验证,实际的VBA代码将比用于使该恶魔字符串易于消化的任何布局更容易维护和调试。

祝你好运!