2

I'm trying to use variables which are strings within an array formula. I've used variables within regular formulas in VBA before absolutely fine but no matter how i try to split the formula up using string concatenation (& and " symbols) it doesn't seem to take. The formula works fine with the variables hard coded in.

The code is within a loop, matching the 3 variables (pulled from another worksheet in the workbook) over 3 columns in the current worksheet. It will then return this row location (j) so data from cells in row j can be used elsewhere.

The formula when used in a cell works fine referencing other cells for the variables. ie -

{=MATCH(1,(A:A=H10)*(B:B=H11)*(C:C=H12),0)}

I've then tried stuff along the lines of the below snippet, but get run-time error '1004'

Dim Var1 As String
Dim Var2 As String
Dim Var3 As String
Dim j As Long
Dim test As String
Set Aw = Application.WorksheetFunction

test = "(A:A = """ & Var1 & """)"

  j = Evaluate(Aw.Match(1, [1*" & test & "*(B:B = "Hard_Coded_Var2")*(G:G = "Hard_Coded_Var3")], 0))

Or just

  test = Var1 

  j = Evaluate(Aw.Match(1, [1* (A:A = & test &) *(B:B = "Hard_Coded_Var2")*(G:G = "Hard_Coded_Var3")], 0))

The following works when the variables are hard coded in

  Dim j As Long
  Set Aw = Application.WorksheetFunction

  j = Evaluate(Aw.Match(1, [1* (A:A = "Hard_Coded_Var1")*(B:B = "Hard_Coded_Var2")*(G:G = "Hard_Coded_Var3")], 0))

Ideally the end result would theoretically be like so -

  j = Evaluate(Aw.Match(1, [1* (A:A = Var1)*(B:B = Var2)*(G:G = Var3)], 0))

I've been getting runtime error 404 'Application define or object defined error'

Thanks so much!

1
  • I get the error even with hard coded variables. Are you sure it works. As far as I know you will need to use the actual formula in quotes not the vba version in evaluate. Commented Aug 22, 2019 at 13:35

1 Answer 1

1

You will need to use a formula string with Evaluate to use an array type formula:

j = Evaluate("MATCH(1,(A:A=""" & Var1 & """)*(B:B=""" & Var2 & """)*(G:G=""" & Var3 & """),0)")
Sign up to request clarification or add additional context in comments.

2 Comments

Scott that is fantastic. Thank you so much.
@TJT if this answered your question, please consider marking as correct by clicking the check mark by the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.