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

时间:2014-11-04 10:50:26

标签: excel vba array-formulas

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

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

我还尝试用With Sheets("Detail analysis").Cells(a, j)替换With Sheets("Detail analysis").Range(Cells(a,j).Address(0,0))命令;但是这仍然会给出FormulaArray错误。

尽管如此,我仍然收到错误:Run-time error: 1004 Unable to set the FormulaArray property of the Range Class问题编辑:显示此错误时,调试器指向以下行:.FormulaArray = formulaP1

有人可以建议我的代码出错吗?

' 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 (i.e. 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行时,代码现在特别失败。注:我检查了formula中的字符数,它们总计达到250(低于MSDN网站http://msdn.microsoft.com/en-us/library/office/ff837104(v=office.15).aspx上规定的255个限制)。

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

1 个答案:

答案 0 :(得分:2)

我将#NULL!改为1E+99,因此永远不会SMALL。不确定#NULL!来自哪里,但它不是可接受的Excel错误代码。我还改变了组装数组公式的方法,选择将其组合为单元格中的字符串,并且只有在进行替换并且公式完全形成后才使其成为数组公式。由于没有要测试的数据和一些变量(样本中缺少值),我想出了这个。

' 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: .Replace功能默认为上次使用的内容。如果这是xlWhole,则.Replace和后续.FormulaArray分配将再次失败。我已修改为指定, lookat:=xlPart参数。