我在一列中有一组电话号码。我想在列中找出类似的内容。相似性规则是前8个数字是相同的,后两个数字是顺序的(至少3个数字)。
例如,
8601116612
8601116613
8601116614
上述三个数字应该被识别为相似。我怎么能在VBA中做到这一点?
答案 0 :(得分:0)
好吧,这有点令人沮丧,但现在是:
Sub findSims()
Dim i, j, rc, rc1 As Double
Dim m, z As Double
i = 1
m = 0
z = 0
Do While Cells(i, 1) <> ""
' calculate the value of the last two digits
' for educational reasons one calculation
rc = Right(Cells(i, 1), 2)
rc1 = Right(Cells(i + 1, 1), 2) - 1
' .. and one direct comparation ..
' .. of the first 8 digits and afterwards the first two
If Left(Cells(i, 1), 8) = Left(Cells(i + 1, 1), 8) Then
If rc = rc1 Then
' there are two counters because we have to wait until we have three similar numbers
m = m + 1
z = z + 1
Else
' if the similarities stop, we kill m
m = 0
' we use z to write this cutie of a sentence (you can insert here whatever you like)
If z > 1 Then
Cells(i, 2) = "The Cells from A" & i & " to A" & (i - z) & " are similar."
End If
z = 0
End If
End If
' increment i and the job is done
i = i + 1
Loop
End Sub
这看起来有点笨重,而且可能是一个更好的解决方案,但这是我想出的最好的解决方案。玩得开心。