VBA:在Vlookup中使用Dims

时间:2015-08-13 20:36:26

标签: excel vba excel-vba vlookup

我试图在VBA中执行vlookup并且我收到Application-defined or object-defined error运行时错误。

我用整数和字符串重写了行而不是Dims,它工作正常,但我需要让它变量。

'Throws Error
rc = -6
tempwb = "Supplier Master - Location - 08-13-15.xls"
acol = 1
zcol = 14
wRange.FormulaR1C1 = "=VLOOKUP(RC[rc],'[" & tempwb & "]Sheet1'!C" & acol & ":C" & zcol & "," & ((zcol - acol) + 1) & ",FALSE)"


'Works
wRange.FormulaR1C1 = "=Vlookup(RC[-6],'[Supplier Master - Location - 08-13-15.xls]Sheet1'!C1:C14,14,FALSE)"

3 个答案:

答案 0 :(得分:4)

尝试更改

wRange.FormulaR1C1 = "=VLOOKUP(RC[rc],

wRange.FormulaR1C1 = "=VLOOKUP(RC[" & rc & "],

完成?

答案 1 :(得分:0)

这样做很容易诊断:

Dim sFormula As String

sFormula = "=VLOOKUP(RC[rc],'[" & tempwb & "]Sheet1'!C" & acol & ":C" & zcol & "," & ((zcol - acol) + 1) & ",FALSE)"

Debug.Print sFormula

wRange.FormulaR1C1 = sFormula

答案 2 :(得分:-1)

尝试更改字符串,以便包含rc变量。

wRange.FormulaR1C1 = "=VLOOKUP(RC[" &rc&"],'["