使用变量引用R1C1公式中的列和范围

时间:2017-01-22 14:37:54

标签: excel-vba vba excel

我试图在R1C1表单中使用Vlookup公式中的变量。我无法获得表数组的语法。

我试过了:

ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[" & -39 - a & "],'Tmp-UPCR'!C1:C&a+1,&a+1,0)" 'Check the formula

我使用的代码是:

Dim a As Integer
For a = 1 To i
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[" & -39 - a & "],'Tmp-UPCR'!C1:C&a+1,&a+1,0)" 'Check the formula
    ActiveCell.Offset(0, 1).Select     
Next a

我怎么能让这个公式起作用?

1 个答案:

答案 0 :(得分:1)

用以下代码替换您的公式:

"=VLOOKUP(RC[" & -39 - a & "],'Tmp-UPCR'!C1:C" & a + 1 & "," & a + 1 & ",0)"

但是,您可以避免所有这些SelectActiveCell,并直接使用下面的For循环:

For a = 1 To i
    ' modify Column A below to the column where you want to put your formula
    Range("A" & i).FormulaR1C1 = "=VLOOKUP(RC[" & -39 - a & "],'Tmp-UPCR'!C1:C" & a + 1 & "," & a + 1 & ",0)" 'Check the formula
Next a