|
- Sub GetTheCity()
- Dim i, j, k, P1, P2, L As Long
- Dim FirstCity, ShengFen As String
- i = Application.Min(1, 2, 3)
- For i = 2 To Application.WorksheetFunction.CountA(Columns(1))
-
- '确定省份名称
- If InStr(Left(Cells(i, 1), 3), "省") <> 0 Then
- P1 = InStr(Left(Cells(i, 1), 3), "省") + 1
- ElseIf InStr(Left(Cells(i, 1), 3), "广西") + InStr(Left(Cells(i, 1), 3), "西藏") + InStr(Left(Cells(i, 1), 3), "宁夏") + InStr(Left(Cells(i, 1), 3), "新疆") <> 0 Then
- P1 = 3
- ElseIf InStr(Left(Cells(i, 1), 3), "内蒙古") <> 0 Then
- P1 = 4
- Else
- P1 = 0
- End If
- '确定 一级市名称的首字符位置
- If P1 = 0 Then
- '找不到省份,就将找 XXX 市
- P2 = InStr(Left(Cells(i, 1), 5), "市")
- Else
- '找到省份或自治区,则找到 首个 市、县、区、州
- For j = 0 To Len(Cells(i, 1))
- If Mid(Cells(i, 1), P1 + j, 1) = "市" Or Mid(Cells(i, 1), P1 + j, 1) = "县" Or Mid(Cells(i, 1), P1 + j, 1) = "区" Or Mid(Cells(i, 1), P1 + j, 1) = "州" Then
- P2 = j
- Exit For
- End If
- Next
- End If
- '确定市名称
- If P1 = 0 Then
- FirstCity = Left(Cells(i, 1), P2 - 1)
- Else
- FirstCity = Left(Right(Cells(i, 1), Len(Cells(i, 1)) - P1 + 1), P2)
- End If
- If P1 = 0 Then
- ShengFen = FirstCity '找不到省行的时候,直接将首个市的名称赋予
- Else
- ShengFen = Left(Cells(i, 1), P1 - 1) '找到省份
- End If
- Cells(i, 2) = ShengFen
- Cells(i, 3) = FirstCity
- Next
- '从字符切割出来的市名称与省名称,从数据源中锁定标准 市、省名称
- For i = 2 To Application.WorksheetFunction.CountA(Columns(1))
- For L = 2 To Application.WorksheetFunction.CountA(Sheet3.Columns(1))
- '找到直辖市,则直接确定为省、市为直辖市
- If InStr(Left(Cells(i, 1), 5), "北京") + InStr(Left(Cells(i, 1), 5), "上海") + InStr(Left(Cells(i, 1), 5), "天津") + InStr(Left(Cells(i, 1), 5), "重庆") <> 0 Then
- Cells(i, 4) = Cells(i, 3)
- Cells(i, 5) = Cells(i, 3)
- Exit For
- End If
- '非直辖市,则 以省、市名称同时匹配数据源
- If InStr(Sheet3.Cells(L, 7), Cells(i, 3)) > 0 And InStr(Sheet3.Cells(L, 2), Cells(i, 2)) > 0 Then
- Cells(i, 4) = Sheet3.Cells(L, 5)
- Cells(i, 5) = Sheet3.Cells(L, 2)
- Exit For
- End If
- Next
- Next
- '补丁:如果 市、省份名称未能匹配, 则以 仅以市名称去匹配了
- For i = 2 To Application.WorksheetFunction.CountA(Columns(1))
- If Cells(i, 4) = "" Then
- For L = 2 To Application.WorksheetFunction.CountA(Sheet3.Columns(1))
- If InStr(Sheet3.Cells(L, 7), Cells(i, 3)) > 0 Then
- Cells(i, 4) = Sheet3.Cells(L, 5)
- Cells(i, 5) = Sheet3.Cells(L, 2)
- Exit For
- End If
- Next
- End If
- Next
- End Sub
复制代码
|
|