VBA: 在 Vlookup 中使用 Dims

VBA: Using Dims in a 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)"

尝试改变

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

成为

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

完成了吗?

尝试更改您的字符串以包含 rc 变量。

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

这样做很容易诊断:

Dim sFormula As String

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

Debug.Print sFormula

wRange.FormulaR1C1 = sFormula