用可变VBA替换RC公式值

时间:2015-11-13 16:13:39

标签: excel vba excel-vba

我有以下内容将一个简单的Vlookup放入一个单元格中。

ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-25],[MasterFood.xlsx]Sheet1!C1:C6,6,0),0)"

我需要将-25替换为已经计算过的变量(称为LastColumn),因为每次运行程序时列号都会更改。代码的完整部分位于

之下
Dim LastColumn As Integer

If WorksheetFunction.CountA(Cells) > 0 Then
    LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    Cells(1, LastColumn + 1).Select
    ActiveCell.FormulaR1C1 = "ORDER"
End If

Cells(2, LastColumn + 1).Select

'Define Categories


For z = 2 To RowCount - 1

ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-25],[MasterFood.xlsx]Sheet1!C1:C6,6,0),0)"
ActiveCell.Offset(1, 0).Select

Next

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你走了:

ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[" & LastColumn & "],[MasterFood.xlsx]Sheet1!C1:C6,6,0),0)"