使用 VBA 生成的公式在工作表之间进行引用

发布于 2024-11-24 08:41:00 字数 465 浏览 0 评论 0原文

我正在尝试使用引用当前工作表和另一个工作表的基本公式填充单元格。

我尝试输入单元格的公式基本上是... ='Sheet2'A(j) / B(i) ,代码如下:

For i = 1 To 5
    For j = 1 To 5
        With shOut.Cells(i,j)
            .formula = "='Sheet2'" & Cells(j,1)).Address & "/" & Cells(i,2).Address
        End With
    Next j
Next i

我遇到问题的部分是工作表在开始时引用;当我在 .formula 中运行它时,它会触发错误。

但是,当我删除“=”符号并将其输出到 .Value 中时,它会打印出我想要的内容,但它不是公式。

I'm trying to populate a cell with a basic formula that references both the current worksheet and another worksheet.

The formula I am trying to input into a cell is basically... ='Sheet2'A(j) / B(i) with the following code:

For i = 1 To 5
    For j = 1 To 5
        With shOut.Cells(i,j)
            .formula = "='Sheet2'" & Cells(j,1)).Address & "/" & Cells(i,2).Address
        End With
    Next j
Next i

The part I am having trouble with is the sheet referencing at the start; when I run this in a .formula, it triggers an error.

However, when I remove the '=' sign and make it output in a .Value, it prints out what I want it to, except it's not a formula.

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

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

发布评论

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

评论(2

不美如何 2024-12-01 08:41:00

您不必自己构建任何范围的地址 - 这样做只会让自己面临潜在的问题。

例如,在 ularis(非常好的)答案中,工作表名称周围没有单引号。在这种情况下,它们不是必需的,但如果工作表名称中包含空格,那么您确实需要引号。 (我的观点是,您不必自己了解并满足所有这些内容)。

相反,您可以在对 Address() 的调用中指定 External:=True,这将为您提供一个包含工作表名称的地址。我会做这样的事情:

Dim oWorksheet1 As Worksheet
Dim oWorksheet2 As Worksheet

...

.Formula = "=" & oWorksheet1.Cells(j,1).Address(External:=True) & "/" & oWorksheet2.Cells(i,2).Address(External:=True)

请注意,在您的情况下,您可能只需要对其中一个工作表使用 External 参数,但这样做额外的时间不会有什么坏处,因为 Excel 会简化无论如何,这个公式适合你。

You shouldn't have to construct the address of any range yourself - you'll only leave yourself open to potential problems by doing so.

For example, in cularis' (perfectly good) answer, there are no single-quotes around the sheet name. They're not required in this case, but if the sheet name had a space in it, then you do need the quotes. (My point is that you shouldn't have to know - and cater for - all this stuff yourself).

Instead, you can specify External:=True in the call to Address(), which will give you an address that includes the sheet name. I'd do something like this:

Dim oWorksheet1 As Worksheet
Dim oWorksheet2 As Worksheet

...

.Formula = "=" & oWorksheet1.Cells(j,1).Address(External:=True) & "/" & oWorksheet2.Cells(i,2).Address(External:=True)

Note that in your case you probably only need to use the External parameter for one of the worksheets, but doing it that extra time won't hurt, as Excel will simplify the formula for you anyway.

终陌 2024-12-01 08:41:00
  1. .formula 开头的行中 ) 过多。
  2. 工作表后面缺少 !

    .formula = "=Sheet2!" &单元格(j,1).地址& “/”&单元格(i,2).地址
    
  1. You have a ) too much in your line that begins with .formula.
  2. You are missing the ! after the sheet:

    .formula = "=Sheet2!" & Cells(j,1).Address & "/" & Cells(i,2).Address
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文