Excel の私の文字列は、213221-sddc や 232323/scdscsd、または数字と文字の間の任意のセパレーターのようなものである可能性があります...このテキストから数字のみを抽出するにはどうすればよいですか?
2194 次
3 に答える
7
正規表現を使用します。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
または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
于 2012-12-13T14:05:36.077 に答える
2
これは、(画像のように)範囲からすべての英数字テキストを読み取ってから、DaveとRichieの上記の関数を使用することから始まります。
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
出力:
于 2012-12-13T12:56:46.103 に答える