根据您需要计数的复杂程度,使用自定义函数可能会更好,下面可以扩展以测量许多标准,但因为它会计算包含“AS TTE”或“PTY LTD”的任何单元格但将每个单元格计为 1:
Public Function CountIfExt(Rng As Range, str1 As String, str2 As String) As Long
Dim c As Range
Dim x As Long
For Each c In Rng
If InStr(c.Value, str1) > 0 Or InStr(c.Value, str2) > 0 Then
x = x + 1
End If
Next c
CountIfExt = x
End Function
将“或”更改为“和”将对包含两个字符串的单元格进行计数,但要检查该单元格是否只包含一次,即排除具有“Trang AS TTE PTY LTD AS TTE”的单元格,然后:(重新阅读您的问题我认为这不是您所追求的,而是为了演示如何扩展该功能...)
Public Function CountIfExt(Rng As Range, str1 As String, str2 As String) As Long
Dim c As Range
Dim x As Long
Dim lstr1 As Long, lstr2 As Long
For Each c In Rng
lstr1 = InStr(c.Value, str1)
lstr2 = InStr(c.Value, str2)
If lstr1 > 0 And lstr2 > 0 Then
If InStr(lstr1 + 1, c.Value, str1) = 0 _
And InStr(lstr2 + 1, c.Value, str2) = 0 Then
x = x + 1
End If
End If
Next c
CountIfExt = x
End Function
希望这能给你足够的帮助,让你到达你需要的地方。