使用行情&在FormulaR1C1中同时变量

时间:2016-12-11 14:51:56

标签: excel-vba vba excel

我的代码就是这个

Sub Macro2()
Dim X As Variant
X = "3.7"
Range("O36").FormulaR1C1 = "=SUM(COUNTIF(INDIRECT({""C9:C34"",""F9:F34""}),"">=""" & Chr(34) & X & Chr(34) & "))"
MSGBOX "Total " & Range("O36").Value & " elements Standard Volts are Greater or Equal to " & X & " Volts"
End Sub

我尝试了"" &的各种组合,但无济于事。上面代码的输出是

  

= SUM(COUNTIF(INDIRECT({" C9:C34"" F9:F34"})"> ="" 3.7&#34))

但粗略只有在

时才会起作用
  

= SUM(COUNTIF(INDIRECT({&#34; C9:C34&#34;&#34; F9:F34&#34;})&#34;&GT; = 3.7&#34))< / p>

2 个答案:

答案 0 :(得分:0)

要正确简单地编写此类字符串,请执行以下操作:

1)以你想要的方式写出字符串,例如:

   Range("O36").FormulaR1C1 = "=SUM(COUNTIF(INDIRECT({"C9:C34","F9:F34"}),">=3.7"))"

2)在开始和结束之间每次出现"都会使它变为双倍,如下所示:

   Range("O36").FormulaR1C1 = "=SUM(COUNTIF(INDIRECT({""C9:C34"",""F9:F34""}),"">=3.7""))"

就是这样!!!!

答案 1 :(得分:0)

使用更直接的方法(双关语)可能会更容易:

Range("O36").Formula = Replace("=SUMPRODUCT((C9:C34>=?)+(F9:F34>=?))", "?", X)

或者

Range("O36").ArrayFormula = Replace("=SUM((C9:C34>=?)+(F9:F34>=?))", "?", X)

您也可以在不使用范围的情况下获得结果:

result = Evaluate(Replace("SUMPRODUCT((C9:C34>=?)+(F9:F34>=?))", "?", X))