ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 如何分离单元格中的文本和数值

如何分离单元格中的文本和数值

作者:绿色风 分类: 时间:2022-08-18 浏览:88
楼主
海绵宝宝
Q:如何分离单元格中的文本和数值?
A:主要采用VBA+正则表达式来提取数据




主要代码如下

  1. Sub 分离数字()
  2. Dim regex As Object
  3. Dim regmatchcollection As Object
  4. Dim regmatch As Object
  5. Dim rng As Range
  6. Dim C As Range
  7. Dim str As String
  8. Set regex = CreateObject("vbscript.regexp")
  9. With regex
  10. .Global = True
  11. .Pattern = "[0-9\.+]"
  12. End With
  13. Set rng = Range("A2:A6")
  14. For Each C In rng
  15. str = ""
  16. Set regmatchcollection = regex.Execute(C.Value)
  17. For Each regmatch In regmatchcollection
  18. str = str & regmatch
  19. Next
  20. C.Offset(0, 1) = str
  21. Next
  22. End Sub

  1. Sub 分离文本()
  2. Dim regex As Object
  3. Dim regmatchcollection As Object
  4. Dim regmatch As Object
  5. Dim rng As Range
  6. Dim C As Range
  7. Dim str As String
  8. Set regex = CreateObject("vbscript.regexp")
  9. With regex
  10. .Global = True
  11. .Pattern = "[^0-9\.+]"
  12. End With
  13. Set rng = Range("A2:A6")
  14. For Each C In rng
  15. str = ""
  16. Set regmatchcollection = regex.Execute(C.Value)
  17. For Each regmatch In regmatchcollection
  18. str = str & regmatch
  19. Next
  20. C.Offset(0, 2) = str
  21. Next
  22. End Sub
1.gif
 

分离字符串的数字和文本.rar
2楼
wise
如何使用正则表达式剔除文本字符串中的数字? http://www.exceltip.net/thread-3121-1-1.html

如何用正则表达式编写自定义提取汉字的函数? http://www.exceltip.net/thread-4772-1-1.html

如何使用正则表达式提取字母和汉字个数? http://www.exceltip.net/thread-4835-1-1.html
3楼
海绵宝宝
小七版主的正则掌握的真好,例如提出中文啊,那个pattern我都看不懂事啥意思,向您学些了

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap