使用暗变量将公式输入范围

时间:2019-02-19 16:56:25

标签: excel vba

我想知道您是否可以帮助我构建特定的宏。我想将用户界面作为现有宏的一部分来构建,以帮助人们构建VLOOKUP公式(因为他们对Excel不太熟悉)。

  • 我可以使用下面的“ FirstSedol”来设置Lookup_Value
  • 然后,我可以通过用户界面框将范围返回为“交易”。
  • 然后我可以将'Trades'范围内的列数作为'ColumnCount'返回,因为我希望VLOOKUP在'Trades'范围的第一列中进行搜索并返回最后一列,因此使用了'ColumnCount' '。

我知道所有零件都是单独工作的,因为我已经使用消息框等返回了它们的结果。但是,我只是无法获得将VLOOKUP公式输入到下面工作表中的单元格H3中的代码(粗体)。您是否有机会提供任何帮助?

Dim Trades As Range 'Prompts user to select range of cells for the VLOOKUP
    Set Trades = Application.InputBox("Select the range of cells from the first sedol on the asset allocation (top left), through to bottom right hand corner of the 'changes' column ", "Select cell range", Type:=8)

Dim ColumnCount
    ColumnCount = Trades.Columns.Count

Dim FirstSedol As Range 'Asks the user to select the first SEDOL code to lookup
    Set FirstSedol = Application.InputBox("Select the first sedol to search for", "Select first sedol", Type:=8)

**Worksheets("Quick Value").Range("H3").Formula = "=Vlookup(FirstSedol, Trades, ColumnCount, FALSE)*-1)"**

非常感谢! 安德鲁

1 个答案:

答案 0 :(得分:1)

您需要从字符串中删除变量,并连接范围的地址。

Worksheets("Quick Value").Range("H3").Formula = "=Vlookup(" & FirstSedol.Address(0,0) & ", " & Trades.Address(0,0) & ", " & ColumnCount & ", FALSE)*-1"