Having Trouble with Formula Array VBA

Pineapple_Crazy

Board Regular
Joined
May 2, 2017
Messages
51
Hey Guys,

I seem to be having some difficulties getting a formula array to work in VBA. I'm not quite sure what I'm doing wrong, but could someone lend a hand? The way I currently have it set up it does nothing.

Thanks very much!

Code:
Sub Template()

'Turn off warnings and popups


Dim StrFile As String
Dim StrPath As String
Dim StrSheetName As String
Dim LastRow As Integer
Dim i As Integer
Dim theFormula1 As String
Dim theFormula2 As String
Dim theFormula3 As String
LastRow = Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row


Range("J12").Value = InputBox("Please enter invoice number.")


StrPath = "Y:\Finance\BI\Pete\JessicaPEARSONTemplate\"
StrFile = Dir(StrPath & "*Invoice*" & "*.xls*")


Do Until StrFile = ""


Workbooks.Open Filename:=StrPath & StrFile
StrSheetName = Sheets(1).Name
    
    With ThisWorkbook.Worksheets("Template")
    
theFormula1 = "=INDEX(F_F_F1(),F_F_F2())"
theFormula2 = "'[" & StrFile & "]" & StrSheetName & "'!R2C3:R2000C3, SMALL(IF(R12C10='[" & StrFile & "]" & StrSheetName & "'!R2C1:R2000C1,"
theFormula3 = "ROW('[" & StrFile & "]" & StrSheetName & "'!R2C1:R2000C1)-ROW('[" & StrFile & "]" & StrSheetName & "'!R2C1)+1), ROW(R[-22]C))"


    With ActiveSheet.Range("A23")
        .FormulaArray = theFormula1
        .Replace "F_F_F1())", theFormula2
        .Replace "F_F_F2()", theFormula3
    
    End With
    End With
    
Workbooks(StrFile).Close savechanges:=False


Range("A23").Select
Selection.AutoFill Destination:=Range("A23:A190"), Type:=xlFillDefault


StrFile = Dir()


Loop
End Sub
 
FYI, your code from Post #5

Try changing this
.FormulaR1C1 = theFormula1
to this
.FormulaArray = theFormula1

And you would need to add an Iferror(yourformula,"") to avoid the #Num ! errors at the bottom of the resulting list.


Jon, I see what you are saying now. I apologize, was confused. This is a great solution and thanks.
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Ok, got the array problem figured out. It's working great thanks Stephen. I'm an idiot. When I exported my file from SSIS it was turning invoice number into text and not a number. The code below works great! Thanks guys!!

Code:
Sub Template()



Dim StrFile As String
Dim StrPath As String
Dim StrSheetName As String
Dim LastRow As Integer
Dim i As Integer
Dim theFormula1 As String
Dim theFormula2 As String
Dim theFormula3 As String


LastRow = Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row




Columns.EntireColumn.Hidden = False
Rows.EntireRow.Hidden = False
    


Range("J12").Value = InputBox("Please enter invoice number below.")




StrPath = "Y:\Finance\BI\Pete\JessicaPEARSONTemplate\"
StrFile = Dir(StrPath & "*Invoice*" & "*.xls*")


Do Until StrFile = ""


Workbooks.Open Filename:=StrPath & StrFile
StrSheetName = Sheets(1).Name




theFormula1 = "=INDEX(F_F_F1(),F_F_F2())"
theFormula2 = "'[" & StrFile & "]" & StrSheetName & "'!R2C3:R2000C3"
theFormula3 = "SMALL(IF(R12C10='[" & StrFile & "]" & StrSheetName & "'!R2C1:R2000C1,ROW('[" & StrFile & "]" & StrSheetName & "'!R2C1:R2000C1)-ROW('[" & StrFile & "]" & StrSheetName & "'!R2C1)+1), ROW(R[-22]C))"
Application.ReferenceStyle = xlR1C1


With ThisWorkbook.Worksheets("Template").Range("A23")


    .FormulaArray = theFormula1
    .Replace what:="F_F_F1()", replacement:=theFormula2, lookat:=xlPart
    .Replace what:="F_F_F2()", replacement:=theFormula3, lookat:=xlPart
   
        
End With
Application.ReferenceStyle = xlA1


Workbooks(StrFile).Close savechanges:=False


Range("A23").Select
Selection.AutoFill Destination:=Range("A23:A190"), Type:=xlFillDefault




StrFile = Dir()


Loop




End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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