Home » excel » excel – FormulaArray issue

excel – FormulaArray issue

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve got an array formula if on one sheet looking at a second sheet, I found that while working on the second sheet everything was slowed down quite a bit because of the array formula’s constant calculating based of the newly imputed data in sheet 2. I really only need it to run when someone goes to sheet one to view the info. I decided I’d program a macro to trigger when the sheet was activated then paste the array formula in to the correct cell, copy the returned value, then paste as a value so that the formula doesn’t remain active.

The issue I’m having is my macro’s don’t seem to want to paste my formula (I’ve even tried using the macro recorder tool to enter the formula and when I try to rerun the recorded macro it fails). I get the “Unable to set the FormulaArray property of the Range class” as my error.

The code is as follows:

Range("U5").Select
Selection.FormulaArray = _
   "=IF(ISERROR(INDEX('Maintenance Setup'!C3,SMALL(IF('Maintenance Setup'!C4=""Switchgear_Switchboard"",ROW('Maintenance Setup'!C4)-ROW(INDEX('Maintenance Setup'!C4,1,1))+1),RC[-1])))=TRUE,"""",INDEX('Maintenance Setup'!C3,SMALL(IF('Maintenance Setup'!C4=""Switchgear_Switchboard"",ROW('Maintenance Setup'!C4)-ROW(INDEX('Maintenance Setup'!C4,1,1))+1),RC[-1])))"

The formula works fine when I enter it in manually, ‘Maintenance Setup’ is the sheet which contains all the data, Switchgear_Switchboard is some text that the document is looking for and there’s a blank in the formula which is “””””

I’ve also tried having the code enter the formula using A1 notation rather then R1C1

Thanks.

How to&Answers:

FormulaArray has a 255 character limit. However, we can work around this limitation by first using place holders for parts of our formula, and then replacing these place holders accordingly.

By the way, using whole column references is very inefficient. I would suggest that you avoid using them.

Also, you can use IFERROR to test whether a formula returns an error, and to return a blank when it does.

Try the following code…

Dim sourceWorksheet As Worksheet
Set sourceWorksheet = Worksheets("Maintenance Setup")

Dim lastRow As Long
Dim formulaPart1 As String
Dim formulaPart2 As String
Dim formulaPart3 As String
With sourceWorksheet
    lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    formulaPart1 = "'" & .Name & "'!$C2:$C" & lastRow
    formulaPart2 = "'" & .Name & "'!$D2:$D" & lastRow & "=""Switchgear_Switchboard"""
    formulaPart3 = "ROW('" & .Name & "'!$D2:$D" & lastRow & ")-ROW(INDEX('" & .Name & "'!$D2:$D" & lastRow & ",1,1))+1"
End With

With Range("U5")
    .FormulaArray = "=IFERROR(INDEX(A_A_A,SMALL(IF(B_B_B,C_C_C),T5)),"""")"
    .Replace "A_A_A", formulaPart1
    .Replace "B_B_B", formulaPart2
    .Replace "C_C_C", formulaPart3
End With