我想從字符串中提取數字。字符串像這樣寫入每個單元格中。Excel VBA:我如何從字符串中提取數字?
1, 1
1, 2
1, 3
數字除以逗號。
我如何提取在Excel VBA中從他們的數字?
非常感謝。
我想從字符串中提取數字。字符串像這樣寫入每個單元格中。Excel VBA:我如何從字符串中提取數字?
1, 1
1, 2
1, 3
數字除以逗號。
我如何提取在Excel VBA中從他們的數字?
非常感謝。
如果我收到了你的問題的權利,你有「1,1」的單元格A1,「1,2」在A2單元格,「1,3」,在單元格A3。
如果你想分別在單元格B1,B2和B3逗號之前的數字,並在細胞中的逗號後C1,C2和C3,你可以做以下的數字:
VBA SOLUTION:
Public Sub test()
'the variables' declaration has been corrected, check
'the post's comments below to find out which correction were made
Dim lngLeft as Long, lngRight as Long, lngCommaPos As Long
Dim intI As Integer
For intI = 1 To 3
lngCommaPos = InStr(1, Range("A" & intI).Value, ",")
lngLeft = Left(Range("A" & intI).Value, lngCommaPos - 1)
lngRight = Right(Range("A" & intI).Value, Len(Range("A" & intI).Value) - lngCommaPos - 1)
Range("B" & intI).Value = lngLeft
Range("C" & intI).Value = lngRight
Next intI
End Sub
NON VBA溶液:
插入單元B1以下公式:
= VALUE(LE FT(A1,FIND( 「 」A1)-1))
插入在小區C1中的以下公式:
= VALUE(RIGHT(A1,LEN(A1) - 查找(「,」, A1)-1))
複製細胞B1和C1 粘貼到B2至C3
細胞如果要在列B和C(字符串代替具有數)可以滴VALUE函數,並在細胞中B1和C1的公式將是
= LEFT(A1,FIND( 「」,A1)-1)
= RIGHT(A1,LEN(A1) - 查找( 「」,A1)-1)
* 假設是所期望的結果是11,12,和13
以下內容已被編寫爲函數,但可以輕鬆更改爲子例程。我不想進入設定範圍,只關注功能。
如果數據只是數字之間用逗號和空格隔開,就用替換:
Function ExtractNumber(ByVal text As String) As String
ExtractNumber = Replace(text, ", ", "")
End Function
如果你想更復雜的功能,將提取物,無論任何的所有數字可能別人在字符串,這是我的RegexExtract函數。默認情況下,我把它,這樣它會逗號獨立的全攻略,但是你可以將它指定爲無:
=RegexExtract(A1, "(\d)", "")
下面是函數:
Function RegexExtract(ByVal text As String, _
ByVal extract_what As String, _
Optional seperator As String = ", ") As String
Application.ScreenUpdating = False
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
Dim i As Long, j As Long
Dim result As String
RE.Pattern = extract_what
RE.Global = True
Set allMatches = RE.Execute(text)
For i = 0 To allMatches.Count - 1
For j = 0 To allMatches.Item(j).submatches.Count - 1
result = result & (seperator & allMatches.Item(i).submatches.Item(j))
Next
Next
If Len(result) <> 0 Then
result = Right$(result, Len(result) - Len(seperator))
End If
RegexExtract = result
Application.ScreenUpdating = True
End Function
I F你不」如果你想使用VBA,你也可以使用文本到列,請參閱:http://support.microsoft.com/kb/214261
你可以使用'Split(value,「,」)' - 這將返回一個變量數組 –
上面的字符串結果是什麼? 11 12 13? – aevanko