运行时错误:1004无法设置Range类的FormulaArray属性

我正试图让VBA在由variables定义的行值的一定范围的单元格中写入一个公式: Arr(,) 。 因为在EXCEL中我会按Ctrl + Shift +input公式,我正在使用FormulaArray命令。 但是我得到: Run-time error: 1004 Unable to set the FormulaArray property of the Range Class

我已经彻底检查了VBA打印公式的string格式是作为一个单元格中的string,并将其与我在EXCEL中的正常input进行比较。 所以公式应该没问题 我已经检查了FormulaArrayinput的长度,并确保它远低于255个字符的限制。 根据在线提供的build议( http://dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/ ),我使用.Replace命令来克服字数限制。

我也试着用With Sheets("Detail analysis").Cells(a, j)replaceWith Sheets("Detail analysis").Cells(a, j)命令。 然而这仍然给出了FormulaArray错误。

不过,我仍然收到错误: Run-time error: 1004 Unable to set the FormulaArray property of the Range Class问题编辑 :当这个错误显示debugging器指向线: .FormulaArray = formulaP1

任何人都可以build议我的错在哪里的代码?

 ' Define variables ' Dim top As Integer Dim bottom As Integer Dim a As Integer Dim sumrows As Double ' Summation of the Main Loads in a list ' Dim totalsum As Double ' Generator Loads total ' Dim etotalsum As Double ' Emergency Generator Loads total ' Dim g As Integer Dim formulaP1 As String Dim formulaP2 As String Dim formulaP3 As String Dim formulaP4 As String Dim nill As String nill = Chr(34) & Chr(34) j = 6 ' Loop for the number of "Actual Load" columns required ' Do While Sheets("Detail analysis").Cells(9, j).Value = Sheets("Detail analysis").Cells(9, 6).Value totalsum = 0 etotalsum = 0 ' Nested Loop for the list ranges identified by the previous code block (ie between orange and blue rows) ' i = 1 Do While Arr(i, 1) <> green ' Green is a previously defined row number ' ''''' Identify the Orange (Top) and Blue (bottom) rows of the current list ' top = Arr(i, 1) bottom = Arr(i, 2) ''''' Write formula in the "Actual Load" column between the Arr() rows ' For a = (top + 1) To (bottom - 1) formulaP1 = "=IF(OR($B" & a + 1 & "=" & nill & ",$A" & a & "=" & nill & "),IF(OR($A" & a & "<>" & nill & ",$B" & a & "<>" & "X_X_X()" formulaP2 = nill & "),$C" & a & "*$D" & a & "*" & Sheets("Detail analysis").Cells(a, j - 1).Address(0, 0) & "," & nill & ")," & "Y_Y_Y()" formulaP3 = "SUM(" & Sheets("Detail analysis").Cells(a + 1, j).Address(0, 0) & ":INDIRECT(ADDRESS(SMALL(IFERROR(IF($A" & a + 2 & ":$A$" & bottom & "<>" & nill & "Z_Z_Z()" formulaP4 = ",ROW($A" & a + 2 & ":$A$" & bottom & ")-1),#NULL!),1),COLUMN(" & Sheets("Detail analysis").Cells(a, j).Address(0, 0) & "),1,1,))))" With Sheets("Detail analysis").Cells(a, j) .FormulaArray = formulaP1 .Replace "X_X_X()", formulaP2 .Replace "Y_Y_Y()", formulaP3 .Replace "Z_Z_Z()", formulaP4 End With Next a Next a i = i + 1 Loop j = j + 2 Loop 

问题编辑下面的一些进一步的试验,我已经尝试VBA编码公式中的一些条件。 这将公式分成两部分:一个语句是=cell*cell*cell ,因此不需要FormulaArray 。 当我运行代码时,这个命令执行得很好。

第二个陈述是考虑一组单元格来计算值的总和。 当我的条件调用FormulaArray行时,代码现在特别失败。 NB我检查了formula的字符数,它们合计为250(小于MSDN网站http://msdn.microsoft.com/en-us/library/office/ff837104(v = office。 15).aspx )。

 ws= Sheets("Detail analysis") With ws formula = "=SUM(" & .Cells(a + 1, j).Address(0, 0) & ":INDIRECT(ADDRESS(SMALL(IFERROR(IF($A" & a + 2 & ":$A$" & bottom & "<>" & nill & _ ",ROW($A" & a + 2 & ":$A$" & bottom & ")-1),1E+99),1),COLUMN(" & .Cells(a, j).Address(0, 0) & "),1,1,))))" End With For a = (top + 1) To (bottom - 1) If ws.Cells(a + 1, 2) = "" Or ws.Cells(a, 1) = "" Then If (ws.Cells(a, 1) <> "" Or ws.Cells(a, 2) <> "") And ws.Cells(a, j - 1) <> "" Then ws.Cells(a, j).formula = "=$C" & a & "*$D" & a & "*" & ws.Cells(a, j - 1).Address(0, 0) End If Else ws.Cells(a, j).FormulaArray = formula End If Next a 

我改变了#NULL! 你不得不1E+99所以它永远不会SMALL 。 不知道哪里#NULL! 来自但它不是一个公认的Excel错误代码。 我也改变了数组公式的组装方法,select把它组合成单元格中的一个string,并且在replace完成并且公式完全形成之后 ,才使它成为一个数组公式。 由于没有数据可以testing,还有一些增值(价值在样本中缺失),我想出了这个。

 ' Write formula in the "Actual Load" column between the Arr() rows ' For a = (top + 1) To (bottom - 1) With Sheets("Detail analysis") formulaP1 = "'=IF(OR($B" & a + 1 & "=" & nill & ",$A" & a & "=" & nill & "),IF(OR($A" & a & "<>" & nill & ",$B" & a & "<>" & "X_X_X()" formulaP2 = nill & "),$C" & a & "*$D" & a & "*" & .Cells(a, j - 1).Address(0, 0) & "," & nill & ")," & "Y_Y_Y()" formulaP3 = "SUM(" & .Cells(a + 1, j).Address(0, 0) & ":INDIRECT(ADDRESS(SMALL(IFERROR(IF($A" & a + 2 & ":$A$" & bottom & "<>" & nill & "Z_Z_Z()" formulaP4 = ",ROW($A" & a + 2 & ":$A$" & bottom & ")-1),1E99),1),COLUMN(" & .Cells(a, j).Address(0, 0) & "),1,1,))))" With .Cells(a, j) .Value = formulaP1 .Replace What:="X_X_X()", Replacement:=formulaP2, lookat:=xlPart .Replace What:="Y_Y_Y()", Replacement:=formulaP3, lookat:=xlPart .Replace What:="Z_Z_Z()", Replacement:=formulaP4, lookat:=xlPart .FormulaArray = .Value End With End With Next a 

Addendunm: .Replacefunction会默认上次使用的function。 如果这是xlWhole.Replace和随后的.FormulaArray分配将再次失败。 我修改了指定, lookat:=xlPart参数。