使用 VBA 在 Excel 中引用变量工作簿

发布于 2024-07-29 20:52:49 字数 328 浏览 3 评论 0原文

我有一个场景,用户创建一个工作簿,然后为该工作簿(我们称之为 A)分配一个变量。 随后,用户创建第二个工作簿(我们称之为 B),并为其分配了另一个变量。 这些工作簿的名称不固定,因此它们始终是变量。

现在我想使用 VBA 在工作簿 A 中对工作簿 B 中包含的值执行 VLOOKUP。 这可能吗? 如果是这样,代码会是什么样子?

这是我的尝试,但在 Excel 中效果不佳:

Range("X7").Formula = "=VLOOKUP(K7,[B]Sheet1!$A:$B,2,FALSE)"

其中 ' B' 是变量名。

谢谢!

I have a scenario where a user creates a workbook, then this workbook (let's call it A) is assigned a variable. Later on down the line, the user creates a second workbook (let's call it B), which is assigned another variable. The names of these workbooks are not fixed, thus they are always variables.

Now I want to do a VLOOKUP in Workbook A of a value contained in Workbook B using VBA. Is this possible? If so, what would the code look like?

Here's my attempt at this, which didn't go over too well with Excel:

Range("X7").Formula = "=VLOOKUP(K7,[B]Sheet1!$A:$B,2,FALSE)"

Where 'B' is the variable name.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攀登最高峰 2024-08-05 20:52:50

您的解决方案很好,只是您忘记了一件事:

"=VLOOKUP(K7,[" & Book "]Sheet1!$A:$B,2,FALSE)"

您需要在 Book 之后添加一个额外的 & 符号:

"=VLOOKUP(K7,[" & Book & "]Sheet1!$A:$B,2,FALSE)"

Your solution is good except you forgot one thing:

"=VLOOKUP(K7,[" & Book "]Sheet1!$A:$B,2,FALSE)"

You need an extra & sign after the Book:

"=VLOOKUP(K7,[" & Book & "]Sheet1!$A:$B,2,FALSE)"
如日中天 2024-08-05 20:52:50

我会这样做:

oCell.Formula = "VLOOKUP(" & oKeyCell.Address & ", " & oSearchRange.Address(External:=True) & ", 2, FALSE)"

换句话说,不是用代码来计算地址,而是让 Excel 来计算。

I would do:

oCell.Formula = "VLOOKUP(" & oKeyCell.Address & ", " & oSearchRange.Address(External:=True) & ", 2, FALSE)"

In other words, rather than calculating what the address is in code, let Excel do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文