设为首页收藏本站

嘻皮客娱乐学习网

 找回密码
 中文注册
搜索
打印 上一主题 下一主题
开启左侧

[OFFICE] VBA批量从字符串中提取数字,中文,英文字母同的

[复制链接]
跳转到指定楼层
楼主
发表于 2016-10-24 10:23:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


第一个参数为要提取数据的单元格,第二个参数为要提取的类型:1-中文,2-英文字母,3-数字。


  1. Function zh(rag As Range, lx As Integer)
  2.   Dim reg As Object
  3.   Set reg = CreateObject("vbscript.regexp")
  4.   tj = Choose(lx, "\w", "[^a-zA-Z]", "\D")
  5.   With reg
  6.     .Global = True
  7.     .Pattern = tj
  8.     zh = .Replace(rag, "")
  9.   End With
  10. End Function
复制代码



或用下面的自定义函数也可以:

  1. Function MyGet(Srg As String, Optional n As Integer = False)  
  2.   
  3.     Dim i As Integer  
  4.     Dim s, MyString As String  
  5.     Dim Bol As Boolean  
  6.       
  7.     For i = 1 To Len(Srg)  
  8.         s = Mid(Srg, i, 1)  
  9.         If n = 1 Then  
  10.             Bol = Asc(s) < 0  
  11.         ElseIf n = 2 Then  
  12.             Bol = s Like "[a-z,A-Z]"  
  13.         ElseIf n = 0 Then  
  14.             Bol = s Like "#"  
  15.         End If  
  16.         If Bol Then MyString = MyString & s  
  17.     Next  
  18.       
  19.     MyGet = IIf(n = 1 Or n = 2, MyString, Val(MyString))  
  20.       
  21. End Function  
复制代码
回复

使用道具 举报

小黑屋|手机版|嘻皮客网 ( 京ICP备10218169号|京公网安备11010802013797  

GMT+8, 2024-6-12 19:17 , Processed in 0.325807 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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