利用正则表达式提取或删除汉字
责任编辑:hylng    浏览:6673次    时间: 2012-12-31 23:16:08      

免职声明:本网站为公益性网站,部分信息来自网络,如果涉及贵网站的知识产权,请及时反馈,我们承诺第一时间删除!

This website is a public welfare website, part of the information from the Internet, if it involves the intellectual property rights of your website, please timely feedback, we promise to delete the first time.

电话Tel: 19550540085: QQ号: 929496072 or 邮箱Email: Lng@vip.qq.com

摘要:在日常的工作应用中,我们经常与汉字打交道,下面这个自定义函数的功能,就是提取与删除字符串或单元格中汉字的自定义函数: Function Hanzi(rng, Optional pd As Boolean = True) As String '******************************************* '时间:2010-09-28 '功能..

分享到:

在日常的工作应用中,我们经常与汉字打交道,下面这个自定义函数的功能,就是提取与删除字符串或单元格中汉字的自定义函数:

Function Hanzi(rng, Optional pd As Boolean = True) As String
'*******************************************
'时间:2010-09-28
'功能:提取给定字符串(单元格)中汉字与非汉字集
'说明:rng  原字符串或单元格
'      pd   当为True时,提取汉字(默认),否则提取非汉字。
'发布:http://www.excelba.com
'******************************** ***********
With CreateObject("VBSCRIPT.REGEXP")
    .Global = True
    If pd Then
        .Pattern = "[^\u4e00-\u9fa5]"
    Else
        .Pattern = "[\u4e00-\u9fa5]"
    End If
    Hanzi = .Replace(rng, "")
End With
End Function

应用示例:设A1里的值为 excel吧 - bengdeng 欢迎您!2010年9月28日

在B1设定公式:=Hanzi(A1)
结果为:吧欢迎您年月日

在C1设定公式::=Hanzi(A1,0)
结果为:excel - bengdeng !2010928

除此,也可以利用到其它的程序中,如下面这段程序是去除当前工作表中所有的汉字:

Sub 去除当前工作表中的汉字()
Dim tRan As Range
Application.ScreenUpdating = False
For Each tRan In ActiveSheet.UsedRange
    tRan = Hanzi(tRan, 0)
Next
Application.ScreenUpdating = True
End Sub

】【打印繁体】【投稿】 【收藏】 【推荐】 【举报】 【评论】 【关闭】【返回顶部