在 VBA Excel 中使用 FormulaR1C1 编写 IF 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9361501/
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-11 15:16:35  来源:igfitidea点击:

Write IF statement using FormulaR1C1 in VBA Excel

excelvbaformula

提问by Carl Thomas

I have the following formula =IF((GLOBAL_DATE-30)<G2,"1 Month",IF((GLOBAL_DATE-60)<G2,"2 Month",IF((GLOBAL_DATE-90)<G2,"3 Month","Older Than 3 Months")))and I would like to write this into specific cells using the FormualR1C1 in VBA. (the GLOBAL_DATE is a named cell on another sheet)

我有以下公式 =IF((GLOBAL_DATE-30)<G2,"1 Month",IF((GLOBAL_DATE-60)<G2,"2 Month",IF((GLOBAL_DATE-90)<G2,"3 Month","Older Than 3 Months"))),我想使用 VBA 中的 FormualR1C1 将其写入特定单元格。(GLOBAL_DATE 是另一个工作表上的命名单元格)

Thanks

谢谢

采纳答案by assylias

This is what I get using the Macro Recorder:

这是我使用宏记录器得到的:

ActiveCell.FormulaR1C1 = _
    "=IF((GLOBAL_DATE-30)<R[1]C[6],""1 Month"",IF((GLOBAL_DATE-60)<R[1]C[6],""2 Month"",IF((GLOBAL_DATE-90)<R[1]C[6],""3 Month"",""Older Than 3 Months"")))"

That is using relative cell addresses (R[1]C[6] is the cell one row below and 6 columns to the rigth from the ActiveCell. Alternatively, you can use absolute adresses by replacing R[1]C[6] by R2C7 (for row 2, column 7 = G2).

即使用相对单元格地址(R[1]C[6] 是 ActiveCell 下方一行和 6 列的单元格。或者,您可以通过将 R[1]C[6] 替换为 R2C7 来使用绝对地址(对于第 2 行,第 7 列 = G2)。

回答by Dick Kusleika

Select the cell that has that formula. In the VBE, go to the Immediate Window and type

选择具有该公式的单元格。在 VBE 中,转到立即窗口并键入

?Activecell.FormulaR1C1

and press enter. That will give you the R1C1 translation of your formula.

并按回车键。这将为您提供公式的 R1C1 翻译。

回答by Patrick Honorez

You can easily use VBA to translate those formulae that you entered in a sheet into a sytax that's suitable for VBA. I once wrote a subfor that purpose.

您可以轻松地使用 VBA 将您在工作表中输入的那些公式转换为适合 VBA 的语法。我曾经为此目的写了一个子