简体   繁体   English

如何在多个工作簿中使用 VBA 变量作为 VLookup 中的查找值

[英]How to use a VBA variable for the lookup value in VLookup across multiple workbooks

I'm trying to retrieve multiple datas from a source workbook to store in a target workbook using VBA.我正在尝试使用 VBA 从源工作簿中检索多个数据以存储在目标工作簿中。 I have managed to do this for a single item of data using the statement:我已经设法使用以下语句对单个数据项执行此操作:

Range("I5").Formula = "=VLookup(G5,'C:\TestFolder\[Test Source V1.0.xlsm]Tickets'!$A$9:$G$17, 4, False)"  

in the immediate window or:在直接窗口中或:

ws.Cells(Row, 9).Formula = "=VLookup(G5,'C:\TestFolder\[Test Source V1.0.xlsm]Tickets'!$A$9:$G$17, 4, False)"  

in the procedure window.在程序窗口中。

However, my final aim is to retrieve several items of data by placing the above statement in a Do Until loop and replacing the G5 lookup value with a variable which gets updated on every pass through the loop.然而,我的最终目标是通过将上述语句放在Do Until loop并用一个变量替换G5查找值来检索几项数据,该变量在每次通过循环时都会更新。

However, when I replace G5 with a variable that scrolls through values ranging from $G$1 , $G$2 , $G$3 , which point to the cells containing the actual lookup values(strings) I get #Name?但是,当我用一个变量替换G5 ,该变量滚动范围从$G$1$G$2$G$3 ,指向包含实际查找值(字符串)的单元格,我得到#Name? errors appearing in the destinations cells.出现在目标单元格中​​的错误。

NB the destination cell range for the retrieved data is also incremented in the loop, see attached picture注意检索数据的目标单元格范围也在循环中递增,见附图

在此处输入图片说明

The picture shows the target file, lookup values are in column G, Remote workbook name in column H and Column I holds the formulas created by the VBA code producing the #NAME?图片显示了目标文件,查找值在 G 列中,远程工作簿名称在H列中, I列包含由生成#NAME?的 VBA 代码创建的公式#NAME? errors错误

Errors错误

I'd guess you've not included quotes around the value you're placing in the formula我猜你没有在公式中的值周围加上引号

Dim S
s = "A1B2D4-222"

ws.Cells(Row, 9).Formula = _
  "=VLookup(""" & S & """,'C:\TestFolder\[Test Source V1.0.xlsm]Tickets'!$A$9:$G$17, 4, False)"  

Thanks Tim, I'm new to VBA and it's taking me a while to get my head around the syntax and methodology.谢谢蒂姆,我是 VBA 的新手,我花了一段时间来了解语法和方法。 Your suggestion didn't work exactly but it certainly pointed me in the right direction and a minor tweak did the trick.你的建议并没有完全奏效,但它确实为我指明了正确的方向,一个小小的调整就成功了。 With your suggestion the following statement put the LookUp Value (Cell Ref) in quotes and so it resulted in #N/A errors as below.根据您的建议,以下语句将查找值(单元格引用)放在引号中,因此导致如下 #N/A 错误。 =VLOOKUP("$G$3",'[Test Source V1.0.xlsm]Tickets'!$A$9:$G$17, 4, FALSE) [Results with errors][1] =VLOOKUP("$G$3",'[Test Source V1.0.xlsm]Tickets'!$A$9:$G$17, 4, FALSE) [有错误的结果][1]

However, removing two sets of double quotes either side of the ampersand, gave me what I needed ie just the cell reference as below.但是,删除&符号两侧的两组双引号,给了我我需要的东西,即如下所示的单元格引用。 ws.Cells(Row, 9).Formula = "=VLookup(" & LookVal & ",'C:\\TestFolder[Test Source V1.0.xlsm]Tickets'!$A$9:$G$17, 4, False)" [Correct formula and Results][2] ws.Cells(Row, 9).Formula = "=VLookup(" & LookVal & ",'C:\\TestFolder[Test Source V1.0.xlsm]Tickets'!$A$9:$G$17, 4, False) " [正确的公式和结果][2]

Many thanks for your help, much appreciated I'm still not sure why the variable would need to be wrapped in ampersands and quotes but will do some further reading.非常感谢您的帮助,非常感谢我仍然不确定为什么变量需要用和号和引号包裹,但会做一些进一步的阅读。 [1]: https://i.stack.imgur.com/c5aaW.png [2]: https://i.stack.imgur.com/2mshS.png [1]: https : //i.stack.imgur.com/c5aaW.png [2]: https : //i.stack.imgur.com/2mshS.png

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

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