Alv*_*ohn 0 excel vba excel-vba
我在excel中的字符串可以是213221-sddc或232323/scdscsd之类的任何东西,或者数字和字符之间的任何分隔符......我怎样才能从这个文本中仅提取数字?
使用正则表达式.我已将strNoAlpha转换为Double,但如果需要,它也可以是字符串.
Dim str As String, strNoAlpha As Double
str = "213221-sddc"
With CreateObject("vbscript.regexp")
.Pattern = "[^\d]+"
.Global = True
strNoAlpha = Trim(.Replace(str, vbNullString))
End With
Run Code Online (Sandbox Code Playgroud)
或者作为UDF:
Function removeAlpha(rng As Range) As Double
If (rng.Count <> 1) Then
removeAlpha = "Only select one cell"
Exit Function
End If
Dim str As String
str = rng.Value2
With CreateObject("vbscript.regexp")
.Pattern = "[^\d]+"
.Global = True
removeAlpha = Trim(.Replace(str, vbNullString))
End With
End Function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1884 次 |
| 最近记录: |