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:
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
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