Excel 运行时错误:1004无法设置Range类的FormulaArray属性
我试图让VBA在特定范围的单元格中编写一个公式,行值由变量定义:Excel 运行时错误:1004无法设置Range类的FormulaArray属性,excel,vba,array-formulas,Excel,Vba,Array Formulas,我试图让VBA在特定范围的单元格中编写一个公式,行值由变量定义:Arr(,)。因为在EXCEL中,我会按Ctrl+Shift+输入公式,所以我使用FormulaArray命令。但是我得到:运行时错误:1004无法设置Range类的FormulaArray属性 我已通过VBA彻底检查了公式的字符串格式打印为单元格中的字符串,并将其与EXCEL中的正常输入进行比较。所以这个公式应该很好。我已经检查了FormulaArray输入的长度,并确保它远低于255个字符的限制。根据online()的建议,我使
Arr(,)
。因为在EXCEL中,我会按Ctrl+Shift+输入公式,所以我使用FormulaArray
命令。但是我得到:运行时错误:1004无法设置Range类的FormulaArray属性
我已通过VBA彻底检查了公式的字符串格式打印为单元格中的字符串,并将其与EXCEL中的正常输入进行比较。所以这个公式应该很好。我已经检查了FormulaArray
输入的长度,并确保它远低于255个字符的限制。根据online()的建议,我使用.Replace
命令来克服单词限制
我还尝试将替换为工作表(“详细分析”).Cells(a,j)
命令替换为替换为工作表(“详细分析”).Range(Cells(a,j).Address(0,0))
;然而,这仍然会产生公式数组错误
尽管如此,我仍然收到错误:运行时错误:1004无法设置Range类的FormulaArray属性
问题编辑:显示此错误时,调试器指向以下行:。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
我更改了#NULL你必须1E+99
这样它就永远不会小了。不确定在哪里#空代码>来自,但它不是可接受的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
“在Arr()行之间的“实际负载”列中写入公式”
对于a=(顶部+1)到(底部-1)
带图纸(“详细分析”)
公式1=“”=IF(或($B”&a+1&“=”&nill&“,$a”&a&“=”&nill&“),IF(或($a”&a&“&nill&“,$B”&a&“&”X_X)()
公式p2=nill&“,$C”&a&“*$D”&a&“*”&.Cells(a,j-1).地址(0,0)&“,”&nill&“,”&“Y__Y()”
公式3=“SUM(&.Cells(a+1,j).Address(0,0)&”:间接(地址(小)(IFERROR)(IF($a“&a+2&“:$a$”)&bottom(”“&nill&“Z_Z_”)
公式4=“,行($A“&A+2&“:$A$”&bottom&“)-1),1E99),1),列(&.单元格(A,j).地址(0,0)和),1,1,)”
带.单元(a,j)
.值=公式1
.替换内容:=“X_X_X()”,替换:=公式2,查看:=xlPart
.替换内容:=“Y_Y_Y()”,替换:=公式3,查看:=xlPart
.替换内容:=“Z_Z_Z()”,替换:=公式4,查看:=xlPart
.FormulaArray=.Value
以
以
下一个
Addendunm:.Replace
功能将默认为上次使用的功能。如果这是xlother
,则.Replace
和随后的.FormulaArray
分配将再次失败。我已修改为指定,lookat:=xlPart
参数。可能是公式字符串中嵌入的引号。也许可以尝试将它们加倍,或者使用Chr(34)来区分它们与关闭字符串?我使用了nill=Char(34)&Char(34)
命令来调用“
。谢谢。我试着替换了#NULL代码>使用非常1E99
以确保它不会像您建议的那样转到SMALL
。然而,我不认为这是问题所在。我也试着按照你建议的方式在单元格中编写公式。我现在在.Value=formulaP1
行出现的错误是“运行时错误'1004'应用程序定义的错误或对象定义的错误”。根据我的经验,这通常与命令的焦点有关,但我找不到任何错误。。。我还尝试将其更改为。Formula=formulaP1
,但没有解决问题。这不允许我进入FormulaArray=.Value
行,因此我可以研究解决方案是否有效。@Galju-您是否在formulaP1=“””=if(或(…
使用一个引号,就像我在单元格中强制输入文本值一样?如果没有,我的道歉…这有点隐藏,可能应该被提及。它总是归结为这么小的事情!不需要道歉,我是错过了这一点的人。代码现在按照您建议的方式顺利运行。非常感谢您的帮助。
' 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