搜档网
当前位置:搜档网 › excel分开同一单元格的数字字母和汉字

excel分开同一单元格的数字字母和汉字

Sub takepart()
Set sh = Worksheets("sheet1")
row = 1
While sh.Cells(row, 1) <> ""
txt = sh.Cells(row, 1) 'A列放原始文字
sh.Cells(row, 2) = reg(txt, "[0-9]+") 'B列写数字
sh.Cells(row, 3) = reg(txt, "[a-zA-Z]+") 'C列写字母
sh.Cells(row, 4) = reg(txt, "[\u4e00-\u9fa5]") 'D列写汉字
row = row + 1
Wend
End Sub

'正则表达式函数

Function reg(txt, key)
Dim oRegExp As Object
Dim oMatches As Object
Dim sText As String
sText = txt
Set oRegExp = CreateObject("vbscript.regexp")
With oRegExp
.Global = True
.IgnoreCase = True
.Pattern = key
Set oMatches = .Execute(sText)
For Each Match In oMatches
reg = reg + Match.Value
Next
End With
Set oRegExp = Nothing
Set oMatches = Nothing
End Function

相关主题