Inserting string in formula array function

jessicsw

New Member
Joined
Jan 16, 2018
Messages
1
Hello! I've been lurking the message board for several weeks and it's been very helpful as I slowly learn more about VBA.

I've read multiple posts on how to incorporate a string into a Formula Array function, but after being stumped for several days, I think I need another pair of eyes to take a look. I keep getting the error: Run-time error 1004 Application-defined or object-defined error in the formula array below:

Code:
Sub FindCAHS()    Dim HS As Range: Set HS = Range("J2")
    Dim refworkbook As Variant
    Dim ref1 As Workbook
    Dim ref2 As Worksheet
    Dim ref3 As String
        refworkbook = Application.GetOpenFilename(".xlsx Files (*.xlsx), *.xlsx", 1, "Select the HS Reference xlsx")
        If refworkbook = False Then Exit Sub
        Set ref1 = Workbooks.Open(refworkbook)
        Set ref2 = ref1.Worksheets.Item(2)
        ref3 = "'" & ref1.Path & Application.PathSeparator & _
    "[" & ref1.Name & "]" & ref2.Name & "'"
            For j = 2 To 3
                If IsEmpty(Cells(j, 10).Value) = True Then
                    HS.Offset(j - 2, 1).FormulaArray = _
                    "=vlookup($F$" & j & "&$G$" & j & "&$H$" & j & ",Choose({1,2},""" & ref3 & """!$C:$C&""" & ref3 & """!$D:$D&""" & ref3 & """!$E:$E, """ & ref3 & """!$F2:$F5000),2,0)"
                    Else: Exit Sub
                End If
            Next
    ref1.Close False
End Sub

When I check the Locals Window, ref3 is recognized as what I set the variable to, but it isn't recognized in the formula. Is the syntax correct? I've been able to run the code without the double quotes and it works, but it will ask me to update the value of ref3 by selecting the workbook and worksheet for every loop.

Jess
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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