使用VBA的variables公式

我正在与这一行代码的语法摔跤。 与“IfError”的公式似乎有错误的语法。 我相信我在正确的地方有引号。

Dim j As Integer Dim MidPointE As String Dim Dist As String Dim Allocation As String MidPointE = "AM" Dist = "AN" Allocation = "AO" for J= 1 to 300 Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE & CustomerLast & ", "")" Cells(j, Allocation).Formula = "=" & Allocation & j & "* S" & CustomerLast Next J 

您也可以同时分配多个公式:

 Range("AN1:AN300").Formula = "=IFERROR(AM1 / AM$" & CustomerLast & ", """")" Range("AO1:AO300").Formula = "=AO1 * S$" & CustomerLast 

我也build议查看Excel表格和结构化参考

为避免循环参考问题,可以计算公式并直接分配值:

 Range("AN1:AN300") = Evaluate("IFERROR(AM1:AM300 / AM$" & CustomerLast & ", """")") Range("AO1:AO300") = Evaluate("INDEX(AO1:AO300 * S$" & CustomerLast & ",)") 

试试这个 :

  Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE & CustomerLast & ", """")" 

string文字中的引号必须被转义 – 这是通过加倍来完成的。

在这里结束了

 & ", "")" 

逗号和)之间的两个引号结束string,并开始一个新的。

 'first string "," 'second string ")" 

既然你不能像这样把两个string扔在一起,我会用CHR等价物replace两个引号。 应该看起来像这样:

 Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE & CustomerLast & ", "& CHR(034) & CHR(034) & ")" 'pretty sure 034 is the ascii code for "