vba 宏 - countif 函数检查范围是否“包含”条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15567275/
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
Macro - countif function checking if the range "contains" the criteria
提问by mgunia
I have a macro which does the countif function checking the criteria from the column B in arange in a column A. Both (range and criteria) might have different size, so the macro goes until the last cell populated. However, it checks exactly the same matches. I would like to transform it to check if the range contains the criateria. For instance, if the criteria is "Manager" and in the range we have "Marketing Manager", I would like to count it. Below you can find the screenshot of the excel file and the code of my macro.
我有一个宏,它执行 countif 函数检查 A 列中 arange 中 B 列的条件。两者(范围和条件)可能具有不同的大小,因此宏会一直运行到填充了最后一个单元格。但是,它会检查完全相同的匹配项。我想转换它以检查范围是否包含标准。例如,如果条件是“经理”并且在我们有“营销经理”的范围内,我想计算它。您可以在下面找到excel文件的屏幕截图和我的宏代码。
Screenshot:
截屏:
Macro Code:
宏代码:
Sub Countif_Until_LastRow()
Dim lastRowColumnB As Long
lastRowColumnB = Range("B65000").End(xlUp).Row
For i = 2 To lastRowColumnB
Cells(i, 3) = Application.CountIf(Range("A:A"), Cells(i, 2))
Next
End Sub
回答by Thanos Darkadakis
Try this:
尝试这个:
Cells(i, 3) = Application.CountIf(Range("A:A"), "*" & Cells(i, 2) & "*")
*(star) works like a wild char
*(star) 像野字符一样工作