macrosExcel Vlookup与范围

我想添加一个macros查看单元格,但是我似乎无法得到函数的参数,也没有范围函数的参数

请帮我说明如何写我的代码的最后一行:

RangeStartRow = 2 RangeStartColumn = 1 + 11 + (3 * (AverageSheetIndex - RowCounter - 1)) RangeEndRow = LastCol RangeEndColumn = 2 + 11 + (3 * (AverageSheetIndex - RowCounter - 1)) ActiveCell.Formula = WorksheetFunction.VLookup(ActiveWorkSheet.Cells(1, c), ActiveSheet.range(Cells(RangeStartRow, RangeStartColumn), Cells(RangeEndRow, RangeEndColumn)), 2, False) 

我相信代码是直的(不要介意前4行的值),我希望在活动单元格上查找上面声明的4个值范围内的单元格(1,c)。

请让我知道如何重写我的代码的最后一行。

提前致谢。

ActiveCell.Formula需要一个string表示你想要在单元格中具有的公式。

  ActiveCell.Formula = "=vlookup(" & activecell.offset(0,1).address & .... 

我通常先在工作表中手动创build公式,然后在“debugging”窗口中键入(对于C3中的公式)

 ? range("C3").formulaR1C1 

然后我编辑公式,如果需要,我复制在我的代码。 使用formulaR1C1避免检索字母:对于$ C $ 4的ActiveCell,

 "=D4" is equivalent to "=RC[1]" "=$D$4" is equivalent to "=R4C4" 

如果您真的想要检索MyCol的列字母,您可以使用:

 split(cells(1,MyCol).address,"$")(1) 

为了帮助你重新修改你的公式,我有时会使用下面的子从debugging窗口调用:

 Sub RngToVba(src As Range) 'Helps to write VBA code that will reproduce formula from existing sheet. 'writes (to the debug window) the VBA code to re-create the formulae in given range 'by Patrick Honorez - www.idevlop.com 'usage: from debug window, type RngToVba [L14:R14] ' or RngToVba range("L14:R14") 'then copy generated code to your module Dim c As Range For Each c In src Debug.Print "range(""" & c.Address & """).formula = """ & _ Replace(c.Formula, """", """""") & """""" Next c End Sub