Adding a variable inside VLookUp

2019-09-01 16:11发布

I'm having a problem with a vlookup function. First code shows that VLOOKUP know what is "TempArg'17", but now I want to change it this text to string and write it inside VLOOKUP, so I created a string temp.

Dim temp As String
Dim num As Int
num = 17

temp = "TempArg" + "'" + Cstr(num)

Here is the old code that works:

ActiveCell.FormulaR1C1 = _
        "=VLOOKUP([@[" & Chr(10) & "Stevilka]],'TempArg''17'!C[-16]:C[-17],2,FALSE)"

And here is the code that I want to change and that doesn't work:

ActiveCell.FormulaR1C1 = _
        "=VLOOKUP([@[" & Chr(10) & "Stevilka]],'" & temp & "'!C[-16]:C[-17],2,FALSE)"

This is what I get.

Run-time error '1004':

Application-defined or object-defined error

1条回答
聊天终结者
2楼-- · 2019-09-01 16:41

There's a missing single quote '.
Change following line:

temp = "TempArg" + "'" + CStr(num)

to

temp = "TempArg" + "''" + CStr(num)

or even better

temp = "TempArg''" & CStr(num)

Also with Dim num As Int this won't run.
Should be Dim num As Integer instead, but I guess this is a typo...

查看更多
登录 后发表回答