简体   繁体   English

Excel公式到VBA代码的转换

[英]Excel Formula to VBA code Conversion

I am trying to create a macro that allows me to scan columns and rows of data and insert a formula into the blank cells. 我正在尝试创建一个宏,该宏允许我扫描数据的列和行并将公式插入空白单元格。 I am able to complete this task with the following excel formula: 我可以使用以下excel公式完成此任务:

=IF(ISBLANK(W4),((IFERROR(DATEDIF(MAX($P4,DATE(2016,5,1)),MIN($Q4,DATE(2016,8,1)),"d"),0)/(DATEDIF(P4,Q4,"d")))*$T4),W4)

My question is, is there a way I can put this into vba code so that I can run a macro that will automatically apply this formula in a column of my excel sheet across 30 rows? 我的问题是,有什么方法可以将其放入vba代码中,以便我可以运行一个宏,该宏将自动将这个公式应用到excel表格的30行中? Therefore, the next row would read: 因此,下一行将显示为:

=IF(ISBLANK(W5),((IFERROR(DATEDIF(MAX($P5,DATE(2016,5,1)),MIN($Q5,DATE(2016,8,1)),"d"),0)/(DATEDIF(P5,Q5,"d")))*$T5),W5)

Thanks in advance for the help! 先谢谢您的帮助!

You can use 您可以使用

Range("RangeToCopyFormulaTo").Formula = Range("CellToCopyFormulaFrom").Formula

Excel will take care of updating the cell references, same as when you copy/paste Excel将负责更新单元格引用,与复制/粘贴时一样

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

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