VBA If Countifs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25894091/
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
VBA If Countifs
提问by DannyBland
I use the below to only instigate a certain piece of code if there is data,
如果有数据,我只使用下面的代码来煽动某段代码,
If WorksheetFunction.CountIf(wksdata.Range("D:D"), "ASM001") > 0 Then
However, I need it to work under a CountIfs as well, as some sheets have more than one criteria, such as the example below where it uses BIR001, BIR004, BIR006, ITI001. I need it to continue if there is at least 1 of ANY of them.
但是,我也需要它在 CountIfs 下工作,因为有些工作表有多个标准,例如下面的示例,它使用 BIR001、BIR004、BIR006、ITI001。如果其中至少有 1 个,我需要它继续。
If WorksheetFunction.CountIfs(wksdata.Range("D:D"), "BIR001", wksdata.Range("D:D"), "BIR004", wksdata.Range("D:D"), "BIR006", wksdata.Range("D:D"), "ITI001") > 0 Then
Can you help locate my error?
你能帮我找出我的错误吗?
回答by DannyBland
I realised I should be adding 4 CountIf functions together.
我意识到我应该将 4 个 CountIf 函数添加在一起。
If (WorksheetFunction.CountIf(wksdata.Range("D:D"), "BIR001") _
+ WorksheetFunction.CountIf(wksdata.Range("D:D"), "BIR004") _
+ WorksheetFunction.CountIf(wksdata.Range("D:D"), "BIR006") _
+ WorksheetFunction.CountIf(wksdata.Range("D:D"), "ITI001")) > 0 Then
Looks a bit messy but does the job!
看起来有点凌乱,但工作正常!
回答by CallumDA
This would be a good way to make it less messy:
这将是一个减少混乱的好方法:
Dim count As Integer
With Application.WorksheetFunction
count = .CountIf(wksdata.Range("D:D"), "BIR001") + _
.CountIf(wksdata.Range("D:D"), "BIR004") + _
.CountIf(wksdata.Range("D:D"), "BIR006") + _
.CountIf(wksdata.Range("D:D"), "ITI001")
End With
If (count > 0) Then
回答by DannyBland
This may be a chance to play with some square brackets but you shouldn't use the full column references.
这可能是使用一些方括号的机会,但您不应该使用完整的列引用。
If CBool([SUMPRODUCT(--(D1:D99999={"BIR001","BIR004","BIR006","ITI001"}))]) Then

