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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,215,576
Messages
6,125,633
Members
449,242
Latest member
Mari_mariou

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