VBA for inserting array formula without looping?

Excel43

New Member
Joined
Jan 7, 2020
Messages
14
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
Hi, I have a macro that inserts an array formula in a big number of cells. It works, but if there is a lot of data (over 3000 rows), it gets super slow and takes about 20 minutes to finish, and very often excel tends to die in the process.

Is it possible to speed it up? If so, how can I achieve that?

This is what I have:

VBA Code:
Sheets("Settings").Range("A" & Rows.count).End(xlUp).Offset(1, 0).Select

     Selection.FormulaArray = _
    "=IFERROR(INDEX(data!R3C8:R3700C8, MATCH(0,IF(ISBLANK(data!R3C8:R3700C8),1,COUNTIF(R1C1:R[-1]C, data!R3C8:R3700C8)), 0)),"""")"

    ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":A600")
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about:

VBA Code:
'
'   Turn Settings off
      Application.ScreenUpdating = False                                            ' Turn Screen Updating off
         Application.Calculation = xlCalculationManual                              ' Turn AutoCalculation off
        Application.EnableEvents = False                                            ' Turn EnableEvents off
    Application.DisplayStatusBar = False                                            ' Turn DisplayStatusBar off
'
'
  With Sheets("Settings")
    LastRowPlus1 = .Range("A" & .Rows.Count).End(xlUp).Row + 1
    .Range("A" & LastRowPlus1 & ":A600").FormulaArray = "=IFERROR(INDEX(data!R3C8:R3700C8, MATCH(0,IF(ISBLANK(data!R3C8:R3700C8),1,COUNTIF(R1C1:R[-1]C, data!R3C8:R3700C8)), 0)),"""")"
  End With
'
'   Turn Settings back on
    Application.DisplayStatusBar = True                                             ' Turn DisplayStatusBar back on
        Application.EnableEvents = True                                             ' Turn EnableEvents back on
         Application.Calculation = xlCalculationAutomatic                           ' Turn AutoCalculation back on
      Application.ScreenUpdating = True                                             ' Turn Screen Updating back on
 
Upvote 0
You cannot enter an array formula into multiple cells at once. So about all you can do is turn calculation off at the start & turn it on again at the end.
 
Upvote 0
You cannot enter an array formula into multiple cells at once. So about all you can do is turn calculation off at the start & turn it on again at the end.
What does that mean? "multiple cells at once"?
 
Upvote 0
What does that mean? "multiple cells at once"?

See the topic 3.2 - Setting a Multi-Cell Array formula

M.
 
Upvote 0
Ok, had some time time to play with this today. Got a chance to run some timings also.

Code from the Original post:

VBA Code:
Sheets("Settings").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select

     Selection.FormulaArray = "=IFERROR(INDEX(data!R3C8:R3700C8, MATCH(0,IF(ISBLANK(data!R3C8:R3700C8),1,COUNTIF(R1C1:R[-1]C, data!R3C8:R3700C8)), 0)),"""")"

    ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":A600")

Timing results:
Original Code Timing_37.6.PNG


Fastest Code that I tested, is actually a loop code:

VBA Code:
    Dim LastRowPlus1    As Long
    Dim RowCounter      As Long
'
    LastRowPlus1 = Sheets("Settings").Range("A" & Sheets("Settings").Rows.Count).End(xlUp).Row + 1
'
    For RowCounter = LastRowPlus1 To 600
        Sheets("Settings").Range("A" & RowCounter).FormulaArray = "=IFERROR(INDEX(data!R3C8:R3700C8, MATCH(0,IF(ISBLANK(data!R3C8:R3700C8),1,COUNTIF(R1C1:R[-1]C, data!R3C8:R3700C8)), 0)),"""")"
    Next

Timing Results:
Loop Option_30.66.PNG


Next fastest option:

VBA Code:
    Dim LastRowPlus1    As Long
'
    LastRowPlus1 = Sheets("Settings").Range("A" & Sheets("Settings").Rows.Count).End(xlUp).Row + 1
'
    Sheets("Settings").Range("A" & LastRowPlus1 & ":A600").FormulaR1C1 = "=IFERROR(INDEX(data!R3C8:R3700C8, MATCH(0,IF(ISBLANK(data!R3C8:R3700C8),1,COUNTIF(R1C1:R[-1]C, data!R3C8:R3700C8)), 0)),"""")"
    Sheets("Settings").Range("A" & LastRowPlus1 & ":A600").FormulaArray = Sheets("Settings").Range("A" & LastRowPlus1 & ":A600").FormulaR1C1

Timing Results:
NoLoopCode_31.3.PNG


Next Fastest Option:

VBA Code:
    Dim LastRowPlus1    As Long
'
    LastRowPlus1 = Sheets("Settings").Range("A" & Sheets("Settings").Rows.Count).End(xlUp).Row + 1
'
    Sheets("Settings").Range("A" & LastRowPlus1).FormulaArray = "=IFERROR(INDEX(data!R3C8:R3700C8, MATCH(0,IF(ISBLANK(data!R3C8:R3700C8),1,COUNTIF(R1C1:R[-1]C, data!R3C8:R3700C8)), 0)),"""")"
    Sheets("Settings").Range("A" & LastRowPlus1).Copy Sheets("Settings").Range("A" & LastRowPlus1 + 1 & ":A600")

Timing Results:
Copy_Paste_Option_34.4.PNG



I'm still not sure what qualifies as "Multiple cells at once", but maybe one of those versions of code qualifies.
 
Last edited:
Upvote 0
I'm still not sure what qualifies as "Multiple cells at once"
It means more than one cell. ;)
In the main an array formula should only be entered into one cell at a time.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top