Souvik Hazra
Souvik Hazra

Reputation: 35

declared value to be used in formula

I need a help with a vba code. I have a code which looks like below:

Sub qdashboard()

Dim usrname As String

uname = "608971221"

ActiveWorkbook.Worksheets("Contract Wise Details").Range("B3").FormulaR1C1 = "=VLOOKUP(uname,Database!C[8]:C[9],2,0)"

Now this brings back #name error. and This "uname" is supposed to be changed user wise.

Any help would be appreciated.

Thanks

Upvotes: 0

Views: 28

Answers (1)

Xabier
Xabier

Reputation: 7735

If replace your code with the one below it should work:

Sub qdashboard()

Dim usrname As String

uname = "608971221"

ActiveWorkbook.Worksheets("Contract Wise Details").Range("B3").FormulaR1C1 = "=VLOOKUP(" & uname & ",Database!C[8]:C[9],2,0)"

You need to pass uname as a variable rather than text, and to do so you concatenate the variable into your formula as I've done above.

Upvotes: 2

Related Questions