简体   繁体   English

如何使用VBA在Excel单元格中设置公式?

[英]How to set a formula in an Excel cell using VBA?

Scott Craner gave me this solution to set a formula in one cell: Scott Craner给了我这个解决方案,以便在一个单元格中设置公式:

Range("F4").Formula = "=IF(D4,B4,"""")"

Now I want to apply that dynamically. 现在,我想动态地应用它。 Here is my translation: 这是我的翻译:

   For i = 4 to 10
       Range("F" & i).Formula = "=IF(D" & i ",B" & i ", """")"
   Next i

This turns red in the editor right away. 立即在编辑器中变成红色。 I need something that will run. 我需要可以运行的东西。 Tnx in advance. 提前Tnx。

What can I do to resolve this? 我该怎么解决?

Like Scott Craner said , You don't need any loop for this. 就像Scott Craner所说的那样 ,您不需要任何循环。

ActiveSheet.Range("F4:F10").Formula = "=IF(D4,B4,"""")"

That will do exactly what you're trying to achieve. 这将完全满足您要实现的目标。 Excel is that smart! Excel真聪明!

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

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