A列 | B列 |
---|---|
手机2134 | |
型号324 | |
2135手机 | |
325型号 | |
2手机136 | |
32型号6 |
遇事不决就去隔壁找Word,将表格A列内容复制到Word中,打开“替换”窗口,输入[0-9]
,点开“高级搜索”勾选“使用通配符”,点击全部替换,将替换好的内容再复制回Excel表格中。
将表格A列内容复制到Word中,打开“替换”窗口,输入[!0-9]
,点开“高级搜索”勾选“使用通配符”,点击“全部替换”,将替换好的内容再复制回Excel表格中。
按Alt+F11,打开VBA编辑器,选中工作薄右键插入模块,在代码窗口粘贴代码。
Function Extract_numbers(aa As Range)
Dim n As Integer, i As Integer, tem
n = Len(aa.Value)
For i = 1 To n
If IsNumeric(Application.Find(Mid(aa.Value, i, 1), "0123456789")) = False Then
tem = tem & Mid(aa.Value, i, 1)
End If
Next
Extract_numbers = tem
End Function
Function Extract_Non_numbers(aa As Range)
Dim n As Integer, i As Integer, tem
n = Len(aa.Value)
For i = 1 To n
If IsNumeric(Application.Find(Mid(aa.Value, i, 1), "0123456789")) = True Then
tem = tem & Mid(aa.Value, i, 1)
End If
Next
Extract_Non_numbers = tem
End Function
按Alt+F11,打开VBA编辑器插入模块,在代码窗口中粘贴代码。
然后关闭VBA编辑器,返回Excel界面,然后按Alt+F8打开“宏”对话框,选择对应宏执行即可。
Sub extra_No()
Dim d As Object, arr, brr, i&
Dim sr As String
Set d = CreateObject("scripting.dictionary")
Set regex = CreateObject("VBScript.RegExp")
arr = [a1:a15]
brr = [b1:b15]
For i = 1 To UBound(brr)
sr = Range("a" & i)
With regex
.Global = True
.Pattern = "\d"
'这里也可以写成.Pattern = "[\u4e00-\u9fa5]"
Range("b" & i) = .Replace(sr, "")
End With
With [b1:b15]
.NumberFormat = "General"
End With
Next
End Sub
Sub extra_No()
Dim d As Object, arr, brr, i&
Dim sr As String
Set d = CreateObject("scripting.dictionary")
Set regex = CreateObject("VBScript.RegExp")
arr = [a1:a15]
brr = [b1:b15]
For i = 1 To UBound(brr)
sr = Range("a" & i)
With regex
.Global = True
.Pattern = "\D"
Range("b" & i) = .Replace(sr, "")
End With
With [b1:b15]
.NumberFormat = "General"
End With
Next
End Sub
参考:
他山之石——在VBA中使用正则表达式-Part2(Regular Expression)
提取一个单元格中的文本(非数字)-自定义函数