VBA Code to Enter Formula in the Cell as Array Formula

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,545
Office Version
  1. 2016
Platform
  1. Windows
Dear Friends,

The purpose of the below code is to enter formulas in the range of cells
It works fine for non Array Formulas for Columns R & S

But it does not work for columns AB, AC & AD because of array formuals I guess - or is it something else ?? because I can't the see the formula results

Can someone pls check


VBA Code:
Private Sub ApplyFormulaToRangeOrders()

    Dim ws As Worksheet
    Dim formulaRange As Range
    
    ' Assign the sheet to a variable
    Set ws = Sheets("ORDERS")

   
    ' Apply formulas to PO & Article Concatenated
    Set formulaRange = ws.Range("R2:R1800")
    formulaRange.Formula = "=IF(ISBLANK(B2),"""",IF(B2<>B1,A2,IF(ISNUMBER(SEARCH(A2,R1)),R1,R1&"" - ""&A2)))"
    
    Set formulaRange = ws.Range("S2:S1800")
    formulaRange.Formula = "=IF(ISBLANK(B2),"""",IF(B2<>B1,F2,IF(ISNUMBER(SEARCH(F2,S1)),S1,S1&"" - ""&F2)))"
    
    ' Now converting formulas to values
    ws.Range("R2:S1800").Value = ws.Range("R2:S1800").Value
            
    ' Apply formulas to the DPI & FRI Columns
    Set formulaRange = ws.Range("AB2:AB1800")
    formulaRange.FormulaArray = "=IFERROR(INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 1""=fri_dpi_labtest_category),0)),"""")"
                
    Set formulaRange = ws.Range("AC2:AC1800")
    formulaRange.FormulaArray = "=IFERROR(INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 2""=fri_dpi_labtest_category),0)),"""")"
            
    Set formulaRange = ws.Range("AD2:AD1800")
    formulaRange.FormulaArray = "=IFERROR(INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 3""=fri_dpi_labtest_category),0)),"""")"
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You'll need to first enter the array formula in the first cell in the range, and then copy or fill down. Here's an example using fill down...

VBA Code:
    ' Apply formulas to the DPI & FRI Columns
    Set formulaRange = ws.Range("AB2:AB1800")
    With formulaRange(1)
        .FormulaArray = "=IFERROR(INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 1""=fri_dpi_labtest_category),0)),"""")"
        .AutoFill Destination:=formulaRange, Type:=xlFillDefault
    End With

Hope this helps!
 
Upvote 0
Solution
or just as below - what do you suggest

VBA Code:
 ws.Range("AA2").FormulaArray = "=IFERROR(INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""DPI 20%""=fri_dpi_labtest_category),0)),"""")"
    ws.Range("AA2:AA1800").FillDown
 
Upvote 0
Just for the hell of it another possible option

VBA Code:
    Set formulaRange = ws.Range("AB2:AB1800")
    With formulaRange
         .Formula = "=IFERROR(INDEX(fri_dpi_labtest_date,MATCH(1,($A2=fri_dpi_labtest_po)*(""FRI 1""=fri_dpi_labtest_category),0)),"""")"
         .FormulaArray = .FormulaR1C1
    End With
 
Upvote 0

Forum statistics

Threads
1,225,375
Messages
6,184,613
Members
453,247
Latest member
scouterjames

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