如何在VBA的公式中插入变量 [英] How can I insert variable into formula in VBA

查看:88
本文介绍了如何在VBA的公式中插入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解决这个问题?

Sub test

Dim i as integer

For I = 1 to 10
   ActiveCell.Offset(0, 2).Formula = "=Sum(E15,&i&)"
Next I

End Sub

推荐答案

你的实际目标不明确

你可能想从这个代码开始

you may want to start form this code

Sub test()
    Dim i As Integer

    For i = 1 To 10
       cells(i, 4).Formula = "=Sum(E" & i & ":E15)"
    Next
End Sub

并根据您的需要进行调整,知道:

and adjust it to your needs, knowing that:

  • 它当前写入单元格D1:D10"

  • it currently writes in cells "D1:D10"

因为 cells(i, 4) 引用了第 4 列(即:列D")4 和 i 行中的单元格,我们处于循环中其中 i 循环从 1 到 10

since cells(i, 4) references a cell in 4th column (i.e.: column "D") 4 and i row, and we're inside a loop where i is looping through 1 to 10

所以如果:

  • 您想引用不同的列,然后只需将 4 更改为正确的列索引

  • you want to reference a different column then just change 4 to the proper column index

您想引用不同的行,然后只需将 i 更改为正确的行索引(如果您需要遍历 1,可能是一些 i+2到 10 但从行 3)

you want to reference a different row then just change i to the proper row index (may be some i+2 if you need to iterate through 1 to 10 but start writing from row 3)

写在这些单元格中的公式是:

the formula written in those cells is:

=SUM(E1:E15) 在 D1 中,

=SUM(E2:E15) 在 D2 中,

....

=SUM(E10:E15) 在 D10 中.

所以只需将 "=Sum(E" & i & ":E15)" 更改为您的实际需求

so just change "=Sum(E" & i & ":E15)" to your actual needs

这篇关于如何在VBA的公式中插入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆