简体   繁体   English

在VBA中记录时更改了Excel IF单元格引用公式

[英]Excel IF cell reference formula being changed when recorded in VBA

I have a macro that works fine. 我有一个工作正常的宏。 I'm adding a formula to it that describes the status of the workbook for someone else to review. 我正在向其中添加一个描述工作簿状态的公式,以供其他人查看。 When I record the macro of implementing the formula into the sheet, VBA records it using relative references even though I do not have the relative references button selected. 当我将实现公式的宏记录到工作表中时,即使我没有选择“相对引用”按钮,VBA也会使用相对引用来记录它。 The relative references do not point correctly so I need to fix it. 相对引用未正确指向,因此我需要对其进行修复。 I checked this post ( adding a dynamic cell reference in vba ) and am now thinking that I should adjust the formula with some VBA reference code but I'm not sure if what the post is using is suitable for me. 我检查了这篇文章( 在vba中添加了动态单元格引用 ),现在想我应该使用一些VBA参考代码来调整公式,但是我不确定该帖子所使用的内容是否适合我。 Am I going in the right direction? 我朝着正确的方向前进吗?

Excel Formula: Excel公式:

=IF(Selections!K2="","Not prepped","Prepped")

When recorded into VBA: 当记录到VBA中时:

ActiveCell.FormulaR1C1 = _
    "=IF(Selections!R[-41]C[-1]="""",""Not prepped"",""Prepped"")"

What I need in VBA code: 我需要在VBA代码中:

ActiveCell.FormulaR1C1 = _
    =IF(Selections!K2="""",""Not prepped"",""Prepped"")

ok, then is this your answer: 好的,这是你的答案:

dim rng as range

set rng = thisworkbook.sheets("Sheet1").range("L5")
rng.Formula = "=IF(Selections!K2="""",""Not prepped"",""Prepped"")"

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

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