简体   繁体   English

指定了Lookup_value列号的VBA Vlookup

[英]VBA Vlookup where Lookup_value column number is specifies

I'm currently writing VBA code which includes a Vlookup. 我目前正在编写包含Vlookup的VBA代码。 The code I have at the moment is this: 我目前的代码是这样的:

ActiveCell.FormulaR1C1 = _ "=VLOOKUP(RC[-7],'[Equities EMIR New.xlsx]Valuation Summary'!R2C4:R100C5,1,FALSE)" ActiveCell.FormulaR1C1 = _“ = VLOOKUP(RC [-7],'[Equiities EMIR New.xlsx] Valuation Summary'!R2C4:R100C5,1,FALSE)”

The problem I have is that the column that the Vlookup will be in a cell in a newly inserted column each time it is run. 我的问题是,每次运行时,Vlookup都会在新插入的列中的单元格中的列。 Therefore having RC[-7] for the lookup value won't work once the macro has been run more than once. 因此,一旦多次运行宏,就无法使用RC [-7]作为查找值。

The lookup value will remain in the same cell (A3), however when I try and replace RC[-7] with A3, I get a #NAME? 查找值将保留在相同的单元格(A3)中,但是当我尝试用A3替换RC [-7]时,是否得到#NAME? error. 错误。

I've been searching for days and I can't find a solution to this, is anyone able to assist? 我已经搜索了几天,但找不到解决方案,有人可以提供帮助吗?

Thanks. 谢谢。

This will solve your problem 这将解决您的问题

ActiveCell.FormulaR1C1 = _ "=VLOOKUP(R3C1,'[Equities EMIRNew.xlsx]Valuation Summary'!R2C4:R100C5,1,FALSE)"

Remember in VBA that when using .FormulaR1C1 to use R1C1 formats for your references. 记住在VBA中,当使用.FormulaR1C1来使用R1C1格式作为参考时。

there is a quick and dirty solution to your problem. 有一个快速而肮脏的解决方案来解决您的问题。 The idea is that FormulaR1C1 expects a string, thus you should indent every time l_counter by -1. 这个想法是FormulaR1C1需要一个字符串,因此每次l_counter都应缩进-1。

Public Sub vlookup()

Dim l_counter As Long: l_counter = 7
'your loops here
    l_counter = l_counter - 1
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-" & l_counter & "],'[Equities EMIR New.xlsx]Valuation Summary'!R2C4:R100C5,1,FALSE)"
 'your loops here
 End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM