使用COUNTIF和INSTR寻求帮助,以确定一组数据中字符串单独出现的次数。我有COUNTIF语句用于单元格值,但是现在我试图深入研究单元格,并确定在整个列中出现了多少个特定字符串的实例。
我的代码如下:
j = 2
Cells(2, 11) = "Active, non-corresp add"
Cells(3, 11) = "No start date of res"
Cells(4, 11) = "Invalid address"
Cells(5, 11) = "Active ID, no country"
Cells(6, 11) = "Invalid address format"
Cells(7, 11) = "Invalid characters in address"
While Cells(j, 11) <> vbNullString
s = WorksheetFunction.CountIf(Worksheets("Addresses Master").Range("N:N"), Cells(j, 11))
If s <> 0 Then
Cells(j, 12) = s
End If
t = WorksheetFunction.CountIfs(Worksheets("Addresses Master").Range("N:N"), Cells(j, 11), Worksheets("Addresses Master").Range("I:I"), 1)
If t <> 0 Then
Cells(j, 13) = t
End If
u = WorksheetFunction.CountIfs(Worksheets("Addresses Master").Range("N:N"), Cells(j, 11), Worksheets("Addresses Master").Range("I:I"), 0)
If u <> 0 Then
Cells(j, 14) = u
End If
k = 15
If Cells(j, 11) = "Review address" Then
p = 0
Else
p = 1
End If
While k <= 19
v = WorksheetFunction.CountIfs(Worksheets("Addresses Master").Range("N:N"), Cells(j, 1), Worksheets("Addresses Master").Range("I:I"), p, Worksheets("Addresses Master").Range("C:C"), Cells(1, k))
If v <> 0 Then
Cells(j, k) = v
v = 0
End If
k = k + 1
Wend
j = j + 1
s = 0
t = 0
u = 0
Wend
在要搜索的单元格中,可能有我要查找的6个字符串的组合(单元格2-7)。
编辑:标题澄清
答案 0 :(得分:0)
通过使用通配符解决了该问题。
不是试图使用InStr这样的东西,而是在搜索单元格位置之前和之后使用星号,从而允许Countif函数在单元格内部进行搜索。
即"*" & cells(j,11) & "*"
然后代码变为:
j = 2
Cells(2, 11) = "Active, non-corresp add"
Cells(3, 11) = "No start date of res"
Cells(4, 11) = "Invalid address"
Cells(5, 11) = "Active ID, no country"
Cells(6, 11) = "Invalid address format"
Cells(7, 11) = "Invalid characters in address"
While Cells(j, 11) <> vbNullString
s = WorksheetFunction.CountIf(Worksheets("Addresses Master").Range("N:N"), "*" & Cells(j, 11) & "*")
If s <> 0 Then
Cells(j, 12) = s
End If
k = 13
p = 1
While k <= 19
v = WorksheetFunction.CountIfs(Worksheets("Addresses Master").Range("N:N"), "*" & Cells(j, 11) & "*", Worksheets("Addresses Master").Range("I:I"), p, Worksheets("Addresses Master").Range("C:C"), Cells(1, k))
If v <> 0 Then
Cells(j, k) = v
v = 0
End If
k = k + 1
Wend
j = j + 1
s = 0
t = 0
u = 0
Wend