VBA:在Vlookup中使用Dims

我想在VBA中做一个Vlookup,我得到一个Application-defined or object-defined error运行时错误。

我重写了整数和string而不是Dims ,它工作正常,但我需要使其variables。

 '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 & "], 

完成了吗?

这样做很容易诊断:

 Dim sFormula As String sFormula = "=VLOOKUP(RC[rc],'[" & tempwb & "]Sheet1'!C" & acol & ":C" & zcol & "," & ((zcol - acol) + 1) & ",FALSE)" Debug.Print sFormula wRange.FormulaR1C1 = sFormula 

尝试改变你的string,所以你包括rcvariables。

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