使用 VBA 输入单元格公式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10178486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:58:04  来源:igfitidea点击:

Enter Cell Formula using VBA

excelvbaquotesformula

提问by Alaa Elwany

I have this simple formula in Cell A1

我在单元格 A1 中有这个简单的公式

=10*Round(B1/10,0)

I know how to enter this in VBA, i simply write:

我知道如何在 VBA 中输入这个,我简单地写:

Range("A1").Formula = "=10*Round(B1/10,0)"

Now, what if I don't know the cell whose number I want to round? In other words, i have an integer variable in my code, N, that can take on multiple values, and I want to round the number in row number N of column B.

现在,如果我不知道要舍入其数字的单元格怎么办?换句话说,我的代码中有一个整数变量 N,它可以取多个值,我想对 B 列的行号 N 中的数字进行四舍五入。

How do I write this? I tried:

我该怎么写?我试过:

Range("A1").Formula = "=10*Round(B" & N & "  & "/10",0)"

But this doesn't work. Tried multiple layouts for the quotes but without success.

但这不起作用。为引号尝试了多种布局,但没有成功。

Can anyone let me know:

谁能告诉我:

  1. How to enter that simple formula? and more importantly
  2. Provide some link/reference that would help me with entering other formulas in the future?
  1. 如何输入那个简单的公式?更重要的是
  2. 提供一些链接/参考,以帮助我将来输入其他公式?

Thanks

谢谢

采纳答案by Doug Glancy

Barry and Excellll have clearly solved your specific question. As to the more general, I've googled around, and was surprised not to find a good general tutorial on this. Here's a short postfrom Daily Dose of Excel.

Barry 和 Excelll 清楚地解决了您的具体问题。至于更一般的,我已经用谷歌搜索了,并且很惊讶没有找到关于这个的好的一般教程。这是 Excel 每日剂量的简短帖子

I'd turn on the macro recorder, create some formuals, see what you get, and then google specific issues. Finally, a good book, such as one of John Walkenbach's "Power Programming in VBA" titles should help.

我会打开宏记录器,创建一些公式,看看你得到了什么,然后谷歌特定问题。最后,一本好书,例如 John Walkenbach 的“Power Programming in VBA”一书应该会有所帮助。