昨天收到一个需求,根据要求生成每个号码的等级(图片)
下面是最后做出的效果
下面是VBA代码
'作者:梁茂业 2019年3月7日
Sub replacePhone1()
'定义起始行
START_ROW = 2
'定义等级
Dim Rng2
Dim level
Dim level_1
Dim level_2
Dim level_3
level_1 = Array(1, 2, 1, 2, 3, 3, 4, 5, 4, 5, 7)
level_2 = Array(0, 1, 1, 2, 3, 1, 2, 3, 3, 4, 5)
level_3 = Array(0, 0, 1, 2, 3, 1, 2, 3, 3, 4, 5)
Set regx = CreateObject("vbscript.regexp")
regx.Global = True
Set Rng = Range("a3:a" & Cells(Rows.Count, 1).End(xlUp).Row)
Rng2 = Sheet2.Range("a2:g" & Sheet2.Cells(Rows.Count, 1).End(xlUp).Row)
For Each rn In Rng
n = n + 1
'基础匹配 是否是手机号
regx.Pattern = "^1\d{10}$"
If regx.Test(rn.Value) Then
'判断号码级别
level = level_3
regx.Pattern = "^(1380772|1380782|1390772|1390782|1980772|1987720|1987722|1987723|1987724|1987725|1987726|1987727|1987728|1987729)"
'第一级
If regx.Test(rn.Value) Then
level = level_1
End If
regx.Pattern = "^(1350772|1360772|1387720|1387721|1387722|1387723|1387724|1387725|1387726|1387727|1387728|1387729|1387820|1387821|1387822|1387823|1387824|1387825|1387826|1387827|1387828|1387829|1397720|1397721|1397722|1397723|1397724|1397725|1397726|1397727|1397728|1397729|1397820|1397821|1397822|1397823|1397824|1397825|1397826|1397827|1397828|1397829|)"
'第二级
If regx.Test(rn.Value) Then
level = level_2
End If
'判断局向
n2 = 0
For i = 1 To UBound(Rng2, 1)
n2 = n2 + 1
n3 = Rng2(n2, 3)
n4 = Rng2(n2, 4)
If rn.Value >= Rng2(n2, 3) And rn.Value <= Rng2(n2, 4) Then
Cells(n + START_ROW, 3) = Rng2(n2, 7)
GoTo area
End If
Next
area:
'尾数顺位9位
regx.Pattern = "(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){8}\d"
If regx.Test(Right(rn.Value, 9)) Then
Cells(n + START_ROW, 4) = "尾数顺位9位"
Cells(n + START_ROW, 2) = "99"
GoTo break
End If
'尾数顺位8位
regx.Pattern = "(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){7}\d"
If regx.Test(Right(rn.Value, 8)) Then
Cells(n + START_ROW, 4) = "尾数顺位8位"
Cells(n + START_ROW, 2) = "99"
GoTo break
End If
'尾数顺位7位
regx.Pattern = "(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){6}\d"
If regx.Test(Right(rn.Value, 7)) Then
Cells(n + START_ROW, 4) = "尾数顺位7位"
Cells(n + START_ROW, 2) = "99"
GoTo break
End If
'尾数连号9位
regx.Pattern = "([\d])\1{8,}"
If regx.Test(Right(rn.Value, 9)) Then
Cells(n + START_ROW, 4) = "尾数连号9位"
Cells(n + START_ROW, 2) = "99"
GoTo break