VBA Excel vlookup循环问题
我在包含订单的 Excel 电子表格中的 VBA 代码有问题。每行包含一个客户编号,我用它来查找客户电子邮件地址,该地址包含在工作簿的不同工作表中。
vlookup 代码对于单个单元格工作正常,但问题是当我尝试循环遍历电子表格的所有行时。单个单元格的 Excel 公式是,例如,
=VLOOKUP(B2,Customers!A2:D1000,4,FALSE)
为此生成的 VBA 代码是:
Range("M2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-11],Customers!RC[-12]:R[999]C[-9],4,FALSE)"
将其合并到循环中,选择起始单元格后,我有以下内容:
Cells(2, 13).Select
Do
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-11],Customers!RC[-12]:R[999]C[-9],4,FALSE)"
ActiveCell.Offset(1, 0).Select
Loop Until IsEmpty(ActiveCell.Offset(0, -10))
问题是我希望“表数组”为 < strong>固定,与正在查找值的单元格无关。但我绝对不知道该怎么做。如果我按如下方式更改代码,则会出现运行时错误:
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-11],Customers!A2:D1000,4,FALSE)"
我尝试过引用、取消引用、设置范围变量、将范围变量与 .address 一起使用...有人请帮忙吗?
太感谢了。
I have a problem with my VBA code in an excel spreadsheet containing orders. Each row contains a customer number, which I use to look up the customer email address, contained in a different sheet in the workbook.
The vlookup code works fine for a single cell, but the problem is when I try to loop through all of the rows of the spreadsheet. The Excel formula for a single cell is, e.g.,
=VLOOKUP(B2,Customers!A2:D1000,4,FALSE)
The VBA code generated for this is:
Range("M2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-11],Customers!RC[-12]:R[999]C[-9],4,FALSE)"
Incorporating this into a loop, after selecting the starting cell, I have the following:
Cells(2, 13).Select
Do
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-11],Customers!RC[-12]:R[999]C[-9],4,FALSE)"
ActiveCell.Offset(1, 0).Select
Loop Until IsEmpty(ActiveCell.Offset(0, -10))
The problem is that I want the "table array" to be fixed, not relative to the cell whose value is being looked up. But I absolutely can't figure out how to do it. If I change the code as follows, I get a run-time error:
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-11],Customers!A2:D1000,4,FALSE)"
I have tried quoting, unquoting, setting a range variable, using the range variable with .address... can someone please help?
Thank you so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定 R1C1 公式中的括号表明您正在指定一个相对范围(特别是其中包含负数)。如果要指定绝对范围,则需要指定R1C1单元格不带括号;例如R2C2:R4C4。
举一个简单的例子:
运行此命令会在单元格 C5 中提供绝对的“A1”式公式。
我认为你的问题可能是:
因为它是一个相对范围。您必须明确指定数据表的位置;例如
Customers!RC12:R999C9
(您需要弄清楚它在工作表上的位置)。解决这个问题的一个简单方法是突出显示工作表上的数据表,然后切换到 Visual Basic 编辑器并手动运行它(将光标放在 Sub 内,然后按“播放”按钮或转到“运行”->“运行”)。跑步)。
I'm pretty sure the brackets in your R1C1 formula indicate that you are specifying a relative range (especially with the negatives in them). If you want to specify an absolute range, you need to specify the R1C1 cells without brackets; e.g. R2C2:R4C4.
As a simple example:
Running this gives you an absolute "A1"-style formula in cell C5.
I think your problem might be:
Because it is a relative range. You have to explicitly specify where your data table is; e.g.
Customers!RC12:R999C9
(you need to figure out where it is on your sheet).An easy way of figuring this out is highlighting your data table on your worksheet, then switch to the Visual Basic Editor and manually run this (put your cursor inside of the Sub, and press the 'Play' button or go to Run->Run).