ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1193|回复: 0

一批身份证地址确定其省份与一级市名称,程序弄好了但效率低下

[复制链接]

TA的精华主题

TA的得分主题

发表于 2016-3-12 10:47 | 显示全部楼层 |阅读模式
  1. Sub GetTheCity()
  2. Dim i, j, k, P1, P2, L As Long
  3. Dim FirstCity, ShengFen As String
  4. i = Application.Min(1, 2, 3)
  5. For i = 2 To Application.WorksheetFunction.CountA(Columns(1))

  6.    
  7.     '确定省份名称
  8.     If InStr(Left(Cells(i, 1), 3), "省") <> 0 Then
  9.         P1 = InStr(Left(Cells(i, 1), 3), "省") + 1
  10.     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
  11.         P1 = 3
  12.     ElseIf InStr(Left(Cells(i, 1), 3), "内蒙古") <> 0 Then
  13.         P1 = 4
  14.     Else
  15.         P1 = 0
  16.     End If

  17.     '确定 一级市名称的首字符位置
  18.    If P1 = 0 Then
  19.         '找不到省份,就将找 XXX 市
  20.        P2 = InStr(Left(Cells(i, 1), 5), "市")
  21.    Else
  22.         '找到省份或自治区,则找到 首个 市、县、区、州
  23.         For j = 0 To Len(Cells(i, 1))
  24.             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
  25.                 P2 = j
  26.                 Exit For
  27.             End If
  28.         Next
  29.     End If

  30.     '确定市名称
  31.     If P1 = 0 Then
  32.         FirstCity = Left(Cells(i, 1), P2 - 1)
  33.     Else
  34.         FirstCity = Left(Right(Cells(i, 1), Len(Cells(i, 1)) - P1 + 1), P2)
  35.     End If
  36.     If P1 = 0 Then
  37.         ShengFen = FirstCity '找不到省行的时候,直接将首个市的名称赋予
  38.     Else
  39.         ShengFen = Left(Cells(i, 1), P1 - 1) '找到省份
  40.     End If
  41.     Cells(i, 2) = ShengFen
  42.     Cells(i, 3) = FirstCity
  43. Next

  44.     '从字符切割出来的市名称与省名称,从数据源中锁定标准 市、省名称
  45.     For i = 2 To Application.WorksheetFunction.CountA(Columns(1))
  46.         For L = 2 To Application.WorksheetFunction.CountA(Sheet3.Columns(1))
  47.             '找到直辖市,则直接确定为省、市为直辖市
  48.             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
  49.                 Cells(i, 4) = Cells(i, 3)
  50.                 Cells(i, 5) = Cells(i, 3)
  51.                 Exit For
  52.             End If
  53.             '非直辖市,则 以省、市名称同时匹配数据源
  54.              If InStr(Sheet3.Cells(L, 7), Cells(i, 3)) > 0 And InStr(Sheet3.Cells(L, 2), Cells(i, 2)) > 0 Then
  55.                 Cells(i, 4) = Sheet3.Cells(L, 5)
  56.                 Cells(i, 5) = Sheet3.Cells(L, 2)
  57.                 Exit For
  58.             End If
  59.         Next
  60.     Next

  61.     '补丁:如果 市、省份名称未能匹配, 则以 仅以市名称去匹配了
  62.     For i = 2 To Application.WorksheetFunction.CountA(Columns(1))
  63.         If Cells(i, 4) = "" Then
  64.         For L = 2 To Application.WorksheetFunction.CountA(Sheet3.Columns(1))
  65.             If InStr(Sheet3.Cells(L, 7), Cells(i, 3)) > 0 Then
  66.                 Cells(i, 4) = Sheet3.Cells(L, 5)
  67.                 Cells(i, 5) = Sheet3.Cells(L, 2)
  68.                 Exit For
  69.             End If
  70.         Next
  71.         End If
  72.     Next
  73. End Sub
复制代码


地址批量查找一级市-excelhome.rar

269.72 KB, 下载次数: 83

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-29 00:55 , Processed in 0.026711 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表