背景::我得到了一个很酷的数组公式,它在Excel中非常有效。现在,我尝试使用VBA做相同的公式。因此,我在单元格中键入了数组公式,并使用宏进行了记录。该公式非常有效。宏记录器告诉我这个:
Selection.FormulaArray = _
"=INDEX('[HOGARES ALBACETE.xlsx]21076'!C1,MATCH(MAX(IF(RIGHT('[HOGARES ALBACETE.xlsx]21076'!C1,LEN(R[-1]C)+2)=""["" &R[-1]C&""]"",'[HOGARES ALBACETE.xlsx]21076'!C2)),IF(RIGHT('[HOGARES ALBACETE.xlsx]21076'!C1,LEN(R[-1]C)+2)=""[""&R[-1]C&""]"",'[HOGARES ALBACETE.xlsx]21076'!C2),0),1)"
如果我尝试运行上面的代码,则会收到错误1004。该子项只有这一行。没什么。
经过一番研究,我发现了这一点:
VBA Run time error 1004: Unable to set the formulaarray property of the range class
Entering Long Array Formulas In VBA
所以我将公式分为两部分:
Dim theFormulaPart1 As String
Dim theFormulaPart2 As String
Dim MiReemplazo As String
MiReemplazo = "cacota"
theFormulaPart1 = "=INDEX('[HOGARES ALBACETE.xlsx]21076'!C1,MATCH(MAX(IF(RIGHT('[HOGARES ALBACETE.xlsx]21076'!C1,LEN(R[-1]C)+2)=""["" &R[-1]C&""]"",'[HOGARES ALBACETE.xlsx]21076'!C2))," & MiReemplazo & ",0),1)"
theFormulaPart2 = "IF(RIGHT('[HOGARES ALBACETE.xlsx]21076'!C1,LEN(R[-1]C)+2)=""[""&R[-1]C&""]"",'[HOGARES ALBACETE.xlsx]21076'!C2)"
With ActiveSheet.Range(“F2”)
.FormulaArray = theFormulaPart1
.Replace MiReemplazo, theFormulaPart2
End With
我没有收到任何错误,但是.Replace MiReemplazo, theFormulaPart2
部分没有执行任何操作(我的意思是,替换没有发生,但是代码已执行)
也尝试过:
ActiveSheet.Range("F2").FormulaArray = theFormulaPart1
DoEvents
Cells.Replace What:=MiReemplazo, Replacement:=theFormulaPart2, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
但是什么都没有。所以我有点主意。
此外,检查两个公式字符串的长度(173,107)。我需要整理字符串吗?
我认为不是问题,
谢谢。
答案 0 :(得分:4)
我敢打赌,您的Excel未设置为使用R1C1引用,因此,当您尝试将R1C1引用的字符串放入A1样式公式中时,替换将不起作用。尝试使用:
Application.ReferenceStyle = xlR1C1
With ActiveSheet.Range("F2")
.FormulaArray = theFormulaPart1
.Replace MiReemplazo, theFormulaPart2
End With
Application.ReferenceStyle = xlA1
答案 1 :(得分:0)
由于强制重新计算了移动的对象,因此这会花费一些时间,但是似乎效果很好。
问题:外部工作簿引用使数组公式超出了字符数限制。
解决方案:a)将外部工作表移至本地工作簿b)完成数组公式的插入c)将本地工作表移至外部工作簿并让Excel找出。
Sub arrayFormulaTooBig()
Dim ha2ndx As Long, wbha As Workbook, wbf As Workbook
Dim sel As Range
Set sel = Selection
Set wbha = Workbooks("HOGARES ALBACETE.xlsx")
Set wbf = sel.parent.parent
'Application.Calculation = xlCalculationmanual
'Application.ScreenUpdating = False
'move the external worksheet to local and reduce worksheet name to minimum characters
With wbha
If .Worksheets.Count = 1 Then
.Worksheets.Add after:=.Worksheets(.Worksheets.Count)
.Worksheets(.Worksheets.Count).Name = "to be removed"
End If
With .Worksheets("21076")
ha2ndx = .Index
.Move after:=wbf.Worksheets(wbf.Worksheets.Count)
End With
End With
'minimize worksheet name
wbf.Worksheets("21076").Name = ChrW(215)
'from 282 characters
'Selection.FormulaArray = _
"=INDEX('[HOGARES ALBACETE.xlsx]21076'!C1,MATCH(MAX(IF(RIGHT('[HOGARES ALBACETE.xlsx]21076'!C1,LEN(R[-1]C)+2)=""["" &R[-1]C&""]"",'[HOGARES ALBACETE.xlsx]21076'!C2)),IF(RIGHT('[HOGARES ALBACETE.xlsx]21076'!C1,LEN(R[-1]C)+2)=""[""&R[-1]C&""]"",'[HOGARES ALBACETE.xlsx]21076'!C2),0),1)"
'to 137 characters
sel.FormulaArray = _
"=INDEX(×!C1,MATCH(MAX(IF(RIGHT(×!C1,LEN(R[-1]C)+2)=""["" &R[-1]C&""]"",×!C2)),IF(RIGHT(×!C1,LEN(R[-1]C)+2)=""[""&R[-1]C&""]"",×!C2),0),1)"
With wbf
With .Worksheets(ChrW(215))
.Move before:=wbha.Worksheets(ha2ndx)
End With
End With
'restore worksheet name
wbha.Worksheets(ChrW(215)).Name = "21076"
On Error Resume Next
Application.DisplayAlerts = False
wbha.Worksheets("to be removed").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub