如何从字母数字文本中提取数字?

我在Excel中的string可以是任何像213221-sddc或232323 / scdscsd或数字和字符之间的任何分隔符…我怎样才能从这个文本只提取数字?

使用正则expression式。 我已经将strNoAlpha作为Double来投射,但如果需要的话,这也可以是一个string。

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 

或者作为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 

这里只是一个开始,你可以阅读从一个范围内的所有字母数字文本(按照图像),然后使用上面提到的function从戴夫和里奇

 Option Explicit Sub TestRange() Dim numberArray As Variant Dim i As Integer Dim strTemp As String numberArray = Application.Transpose(Sheets(1).Range("B4:B11")) For i = LBound(numberArray) To UBound(numberArray) strTemp = numberArray(i) numberArray(i) = ExtractNumber(strTemp) '-- **You use above mentioned function** Next i 'Output the processed array into the Sheet. Sheets(1).Range("C4").Resize(UBound(numberArray), _ LBound(numberArray)) = Application.Transpose(numberArray) End Sub 

输出:

在这里输入图像说明