Excel交流网
  • 设为首页|收藏本站|手机版
  • Excel-教程-技巧-培训视频

  • 网站首页
  • Excel教程
  • 新闻动态
  • Excel资源
  • 关于我们

Excel教程

Excel操作
Excel函数
Excel图表
Excel VBA
Excel 行业应用

联系方式

Excel中交流网 联系方式

QQ:18449932 


网  址:www.excel-cn.com  

当前位置:网站首页 > Excel教程 > Excel VBA
Excel VBA

用Excel VBA 通过正则表达式从一串字符中提取所有数字或提取指定长度的数字

作者:tmtony

用Excel VBA 通过正则表达式从一串字符中提取所有数字或提取指定长度的数字

有一个客户有这个需求,就帮忙做了2个函数

废话少说,直接上代码


'基础版 提取字符中的所有数字

Public Function MyGetNum(strSrc As String) As String

  Dim regex As Object

  Set regex = CreateObject("vbscript.regexp")

  With regex

    .Global = True

    .Pattern = "[^\d+]" '替换掉非数字

 

    MyGetNum = .Replace(strSrc, "")

    

  End With

  

End Function


 


'升级版 只取连续的16位的数字

Public Function MyGetNum2(strSrc As String) As String

  Dim regex As Object

  Dim matcher As Object

  Dim i As Integer

  Set regex = CreateObject("vbscript.regexp")

  With regex

      .Global = True

    .ignorecase = True

    .Pattern = "\d{16}"

    

  End With

  Set matcher = regex.Execute(strSrc)

  For i = 0 To matcher.Count - 1

    Debug.Print matcher.Item(i)

  Next i

  

End Function


点击次数:  更新时间:2017-04-11 12:08:52  【打印此页】  【关闭】
上一条:自杀程序  下一条:Excel获取所有工作表名
本站动态|在线留言|在线反馈|友情链接|会员中心|站内搜索|网站地图|网站管理

中山市天鸣科技发展有限公司 版权所有 1999-2024 粤ICP备10043721号

QQ:18449932

免费Excel教程、Excel技巧、Excel培训、Excel函数公式、Excel图表、Excel VBA

Excel教程|Excel技巧|Excel培训|Excel函数公式|Excel图表|VBA

Powered by MetInfo 5.3.12 ©2008-2026  www.metinfo.cn