简体   繁体   English

使用VBA插入公式-带有空白的IF语句

[英]Using VBA to insert formula - IF statement with blanks

I am using VBA to enter a formula into a range of cells. 我正在使用VBA在一系列单元格中输入公式。 The formula is: 公式为:

If VAT = "YES" Then
Range("G24:G47").Formula = "=IF(F24="","",G24*0.14)"
ElseIf VAT = "NO" Then
Range("G24:G47").Formula = "=IF(F24="","",G24*0)"
Else
'Do Nothing
End If

Basically it checks the variable VAT to see whether it is YES or NO and then inserts the corresponding formula. 基本上,它会检查变量VAT以查看是YES还是NO,然后插入相应的公式。 The issue is that the formulas for both options output as follows (I am using VAT = YES as the example): =IF(F24=",",G24*0.14) . 问题是两个选项的公式输出如下(我以VAT = YES为例): =IF(F24=",",G24*0.14) The problem is that one " disappears from the logic step and the if true portion. Any help on this would be really appreciated. 问题是,一个"从逻辑步骤和如果属实部分消失。任何帮助将非常感激。

You need to double all the double quotes used in the formula. 您需要将公式中使用的所有双引号都加倍。 eg "" would become """" and if you have something like "Yes" in the formula, it would become ""Yes"". 例如,“”将变为“””,并且如果公式中的内容类似于“是”,则它将变为“是”。

Range("G24:G47").Formula = "=IF(F24="""","""",G24*0.14)"

You can just straight ignore the NO like this : 您可以像这样直接忽略NO:

If VAT = "YES" Then
Range("G24:G47").Formula = "=IF(F24="","",G24*0.14)"
Else
Range("G24:G47").Formula = "=IF(F24="","",G24*0)"
End If

otherwise 除此以外

If VAT = "YES" Then
Range("G24:G47").Formula = "=IF(F24="","",G24*0.14)"
ElseIf VAT = "NO" Then
Range("G24:G47").Formula = "=IF(F24="","",G24*0)"
Else
 Exit Sub
End If

Whatever type of "code block" you're currently in, you have "Exit" the same kind, so "Exit Sub", "Exit Function", etc. 无论您当前使用哪种类型的“代码块”,都具有相同的“退出”类型,因此,“退出子”,“退出功能”等等。

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

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