ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[2007] 将姓名和电话号码拆分

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-8-5 13:09 | 显示全部楼层
我发一个VBA 灵活性代码  可以作为工具使用
  1. Sub 正则替换()
  2.    
  3.   
  4.     On Error Resume Next
  5.     Set regx = CreateObject("vbscript.regexp")
  6. 1:
  7.     Set sjq = Application.InputBox(prompt:="请选择:查找区域", Type:=8)
  8.     If sjq Is Nothing Then GoTo ff1
  9.     sjqzhs = sjq.Rows.Count
  10.     sjqqsl = sjq.Column
  11.     sjqqsh = sjq.Row
  12.     Set jgq = Application.InputBox(prompt:="请选择:结果摆放起始位", Type:=8)
  13.     If jgq Is Nothing Then GoTo ff1
  14.     Application.ScreenUpdating = False
  15.     jgqqsh = jgq.Row
  16.     jgql = jgq.Column
  17.     arr = sjq
  18.     With regx
  19.         .Global = True '默认为 false  只查找第一个  True则全部查找
  20.         bds = Application.InputBox(prompt:="请输入:正则表达式")
  21.         .Pattern = bds 'Pattern 是属性 正则表达式的内容
  22.         bb = MsgBox("是全部替换还是保留 是为替换 否为保留", vbYesNo)
  23.         If bb = 7 Then GoTo 11
  24.         th = Application.InputBox(prompt:="替换为:什么")
  25.         zcljzf = Application.InputBox(prompt:="请输入最初连接字符:什么")
  26.         jwljzf = Application.InputBox(prompt:="请输入最末连接字符:什么")
  27.         If sjqzhs > 1 Then GoTo 2
  28.         Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  29.         Cells(jgqqsh + n, jgql) = zcljzf & .Replace(arr, th)
  30.         GoTo ff
  31. 2:
  32.         For Each Rng In arr
  33.             Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  34.             Cells(jgqqsh + n, jgql) = zcljzf & .Replace(Rng, th) & jwljzf 'Replace为一种方法 此种方法为替换
  35.             If Cells(jgqqsh + n, jgql) = zcljzf & jwljzf Then Cells(jgqqsh + n, jgql) = ""
  36.             If zcljzf & Cells(sjqqsh + n, sjqqsl) & jwljzf = Cells(jgqqsh + n, jgql) Then Cells(jgqqsh + n, jgql).NumberFormatLocal = "@": Cells(jgqqsh + n, jgql) = Cells(sjqqsh + n, sjqqsl)
  37.             n = n + 1
  38.         Next
  39.         GoTo ff
  40. 11:
  41.         bbb = MsgBox("是保留全部还是一个 是为全部 否为一个", vbYesNo)
  42.         If bbb = 7 Then GoTo 12
  43.         If sjqzhs > 1 Then GoTo 3
  44.         fgf = Application.InputBox(prompt:="请输入:分隔符")
  45.         Set matc = .Execute(arr)
  46.         sl = matc.Count
  47.         For Each mm In matc
  48.             kk = kk + 1
  49.             If kk = sl Then GoTo 15
  50.             Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  51.             Cells(jgqqsh + n, jgql) = mm2 & mm & fgf
  52.             mm2 = mm2 & mm & fgf
  53.             GoTo 17
  54. 15:
  55.             Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  56.             Cells(jgqqsh + n, jgql) = mm2 & mm
  57. 17:
  58.         Next mm
  59.         GoTo ff
  60. 3:
  61.         fgf = Application.InputBox(prompt:="请输入:分隔符")
  62.         For Each m1 In arr
  63.             Set k = regx.Execute(m1) 'Execute方法:返回匹配成功的结果,是一个对象
  64.             js = k.Count
  65.             For Each m In k
  66.                 xhcs = xhcs + 1
  67.                 If js = 1 Then GoTo 33
  68.                 If js > 1 And xhcs = 1 Then GoTo 33
  69.                 If xhcs = js Then GoTo 35
  70.                 Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  71.                 Cells(jgqqsh + n, jgql) = m2 & m & fgf
  72.                 m2 = m2 & m & fgf
  73.                 GoTo 34
  74. 33:
  75.                 Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  76.                 Cells(jgqqsh + n, jgql) = m
  77.                 m2 = m & fgf
  78.                 GoTo 34
  79. 35:
  80.                 Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  81.                 Cells(jgqqsh + n, jgql) = m2 & m
  82. 34:
  83.             Next
  84.             m2 = ""
  85.             xhcs = 0
  86.             n = n + 1
  87.         Next
  88.         GoTo ff
  89. 12:
  90.         If sjqzhs > 1 Then GoTo 4
  91.         fgf = Application.InputBox(prompt:="请输入:分隔符")
  92.         Set matc = .Execute(arr)
  93.         sl = matc.Count
  94.         For Each mm In matc
  95.             kk = kk + 1
  96.             If kk = sl Then GoTo 5
  97.             Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  98.             Cells(jgqqsh + n, jgql) = mm2 & mm & fgf
  99.             mm2 = mm2 & mm & fgf
  100.             GoTo 7
  101. 5:
  102.            Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  103.             Cells(jgqqsh + n, jgql) = mm2 & mm
  104. 7:
  105.         Next mm
  106.     End With
  107.     GoTo ff
  108. 4:
  109.     For Each m1 In arr
  110.         Set k = regx.Execute(m1)
  111.         For Each m In k
  112.             ii = ii + 1
  113.             If ii > 1 Then GoTo 36
  114.             Cells(jgqqsh + n, jgql).NumberFormatLocal = "@"
  115.             Cells(jgqqsh + n, jgql) = m
  116.         Next
  117. 36:
  118.         ii = 0
  119.         n = n + 1
  120.     Next
  121.    
  122. ff:
  123.     n = 0
  124.      Application.ScreenUpdating = True
  125.     aa = MsgBox("是否还需继续处理", vbYesNo)
  126.     If aa = 6 Then GoTo 1
  127. ff1:
  128.   Application.ScreenUpdating = True
  129. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2018-8-7 12:02 | 显示全部楼层
cjc209 发表于 2018-8-5 13:09
我发一个VBA 灵活性代码  可以作为工具使用

功能好强大,但是正则,VB不会写

TA的精华主题

TA的得分主题

发表于 2018-8-7 15:38 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
jd23qq 发表于 2018-8-7 12:02
功能好强大,但是正则,VB不会写

这个 只要你知道匹配代码就可以使用了
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

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

GMT+8, 2024-4-25 00:44 , Processed in 0.036357 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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