VBA - Executing array formulas

NathanA

New Member
Joined
Jan 18, 2017
Messages
33
Hello,

The code below was written to update formulas in F5:N and array formulas in H5:AA, and replace the formulas in F6:A6 and below with the output as values. I am using VBA code to avoid a slow workbook, forcing the formulas to run only after I have updated other data in the workbook.

The code updates the formulas correctly, however the array formulas in rows 6 and below return #N/A. I have searched online and tried adding Array to Formula (FormulaArray) however this returns an error message. I have checked possible reasons for this error and for example, the array formulas are less than the maximum 255 characters. Is it possible to amend the code below to successfully run the array formulas?

Code:
Option Explicit


Sub RefreshData()


'Declare variables
Dim desWS As Worksheet, i As Long, LastRow As Long


'Set the sheet
Set desWS = Sheets("View")


    With desWS
        LastRow = .Range("D" & .Rows.Count).End(xlUp).Row
        .Range("F6:AA" & .Rows.Count).ClearContents
        For i = 6 To 27
            Application.StatusBar = "Updating column " & i & " of 27"
            DoEvents
            
            .Range(.Cells(5, i), .Cells(LastRow, i)).Formula = .Cells(5, i).Formula
            .Range(.Cells(6, i), .Cells(LastRow, i)).Value = .Range(.Cells(6, i), .Cells(LastRow, i)).Value
        Next i
    End With
    
End Sub
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
11,101
Office Version
2013
Platform
Windows
Did you try this?

Code:
.Cells(5, i).Formula.Copy .Range(.Cells(5, i), .Cells(LastRow, i))
 

NathanA

New Member
Joined
Jan 18, 2017
Messages
33
Hi,

Thanks for looking into this. I get an error message (Object required) when I use that code.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,476
Office Version
365
Platform
Windows
What if you try
Code:
.Range(.Cells(5, i), .Cells(LastRow, i)).FillDown
 

NathanA

New Member
Joined
Jan 18, 2017
Messages
33
Thank you, that works really well.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,476
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

MARK858

Well-known Member
Joined
Nov 12, 2010
Messages
11,344
Office Version
365, 2010
Platform
Windows, Mobile
Just out of personal interest does the below work for you?

Code:
    Dim x As String
    x = .Cells(5, i).Formula
    With .Range(.Cells(5, i), .Cells(LastRow, i))
        .Formula = x
        .FormulaArray = .FormulaR1C1
    End With
 
Last edited:

Forum statistics

Threads
1,078,370
Messages
5,339,789
Members
399,325
Latest member
smilevenki

Some videos you may like

This Week's Hot Topics

Top