0

I have this code:

currName = "string" 'unused
cellNum = [VLOOKUP("string", '2012'!A:M, 13, FALSE)]

But I need to replace "string" with a variable in the VBA code named currName. I tried this:

currName = "string" 'used
cellNum = [VLOOKUP(currName, '2012'!A:M, 13, FALSE)]

What's the appropriate syntax? When I try the 2nd part with using the variable the issue I'm getting is it's returning invalid data (like if I input it back into a cell, it's #NAME?). currName is simply equal "string". It works fine in the first example without the variable being used.

4
  • Where is the rest of your code? It's pretty impossible to figure out what might be wrong (I can think of 3 or 4 things) without seeing how your variables are declared, etc. Commented Jul 18, 2013 at 16:47
  • @DavidZemens Added; it's nothing complex just an assignment. Commented Jul 18, 2013 at 17:03
  • See my answer below, and read @DougGlancy's comments for an explanation of why your approach fails when not using a string literal in the function. Commented Jul 18, 2013 at 17:52
  • Have you had a chance to try my answer below? Commented Jul 27, 2013 at 15:23

2 Answers 2

3

I've not seen square brackets used in this manner before. Ordinarily those are shortcut for Range objects. I am able to replicate your error condition.

You can use something like this, instead. Both assignments to cellNum work without error:

Sub Test2()
Dim wsFunc As WorksheetFunction: Set wsFunc = Application.WorksheetFunction
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
Dim rngLook As Range: Set rngLook = ws.Range("A:M")
Dim cellNum As Range
Dim currName As String

currName = "string" 'unused

Set cellNum = ActiveCell

cellNum = wsFunc.VLookup("string", rngLook, 2, False)

cellNum = wsFunc.VLookup(currName, rngLook, 2, False)

End Sub
Sign up to request clarification or add additional context in comments.

11 Comments

FYI, The square brackets are actually more a shortcut for VBA's Evaluate function. The don't work in the OP because i doesn't evaluate to anything within the formula. +1 on your answer.
@DougGlancy I didn't see any i in the OP formula?
Oops, that was my simplified testing. I meant the variable CurrName.
@DougGlancy -- why not? I think that must be the root problem in OP's question. There is an assignment statement of currName = "string" in the preceding line. Why would it not evaluate?
Because it would be like putting that whole formula into a cell. CurrName makes no sense in that context. There's no evaluation within the Evaluate, in other words it doesn't know that CurrName is a variable that's set to "String". Again, it's just like if you entered that whole formula in a cell. And you are right, that's the root of the OP's problem.
|
1

Use the Evaluate function. The formula is a string itself, so you have to concatenate everything into a string:

cellNum = Evaluate("VLOOKUP(""" & currName & """, '2012'!A:M, 13, FALSE)")

6 Comments

I'm not using the =VLOOKUP() syntax, could you translate that into the way I'm using it?
Did you notice the result of what you're trying is "string"?
But no cells in my worksheet have formulas, and your code seems like it's trying to insert a formula into a cell. That isn't what I'm doing.
Sorry for not undertanding exaclty what you wanted, but now it works. The Evaluate function does the same thing the brackets, but takes a string as parameter, so you can concatenate like done in answer.
I'm still getting the #NAME? error when I try this. Are you sure you're not missing a set of quotes?
|

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.