|
- Sub 提取()
- Set d = CreateObject("scripting.dictionary") '全部
- Set d1 = CreateObject("scripting.dictionary") '全部市
- Set d2 = CreateObject("scripting.dictionary") '全部省
- arr = Sheets("省市县代码对照表").[a1].CurrentRegion
- ReDim brr(1 To UBound(arr), 1 To 3) '省-市-县的对应关系
- For i = 2 To UBound(arr)
- x = arr(i, 4) '代码
- dm = arr(i, 3) '地名
- d(x) = dm
- If Right(x, 2) <> "00" Then '最后两个非00,县级
- n = n + 1
- brr(n, 1) = d(Left(x, 2) & "0000") '省级
- brr(n, 2) = d(Left(x, 4) & "00") '市级
- If brr(n, 2) = "市辖区" Or brr(n, 2) = "县" Or brr(n, 2) = "市" Then brr(n, 2) = ""
- If Len(brr(n, 2)) > 0 Then d1(brr(n, 2)) = brr(n, 1) '全部市对应省
- d2(brr(n, 1)) = "" '全部省
- brr(n, 3) = dm '县级
- End If
- Next
-
- For i = 1 To n - 1 '县级名称按字符长度排序(因为用instr比较,会把短名包含在长名中,所以要先检查是否有长名)
- For j = i + 1 To n
- If Len(brr(i, 3)) < Len(brr(j, 3)) Then
- For P = 1 To 3
- tmp = brr(i, P): brr(i, P) = brr(j, P): brr(j, P) = tmp
- Next
- End If
- Next
- Next
-
- arr = Sheet1.Range("b3:b" & Sheet1.[b65536].End(3).Row)
- ReDim crr(1 To UBound(arr), 1 To 3) '结果数组
- For i = 1 To UBound(arr)
- x = arr(i, 1) '长地名
- For k = 1 To n '先查县
- dm = brr(k, 3)
- If InStr(x, dm) > 0 Then
- crr(i, 1) = brr(k, 1)
- crr(i, 2) = brr(k, 2)
- crr(i, 3) = dm
- Exit For
- End If
- Next
-
- If k > n Then '查不到县查市
- f = 0
- For Each dm In d1.keys
- If InStr(x, dm) > 0 Then
- crr(i, 1) = d1(dm)
- crr(i, 2) = dm
- f = 1
- Exit For
- End If
- Next
-
- If f = 0 Then '查不到市查省
- For Each dm In d2.keys
- If InStr(x, dm) > 0 Then
- crr(i, 1) = dm
- Exit For
- End If
- Next
- End If
- End If
- Next
- Sheet1.[c3].Resize(i - 1, 3) = crr
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|