我在excel中的字符串可以是任何像213221-sddc或232323/scdscsd或数字和字符之间的任何分隔符......我怎样才能从这个文本中提取数字?如何从字母数字文本中提取数字?
回答
这仅仅是一个开始,你可以从一系列阅读你的所有字母数字文字(根据图像),然后使用ABOVE MENTIONED FUNCTION From Dave and 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
输出:
使用正则表达式。我已经将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
+ 1用于RegEx。你可能也想使用'Trim(strNoAlpha)'去除空格? –
空格被\ d删除,只返回数字,但通常是一种很好的技术。 – InContext
'strNoAlpha = .Replace(str,vbNullString)'不会删除空格...试试这个'Debug.Print Len(Trim(strNoAlpha))'和'Debug.Print Len(strNoAlpha)'第一个会给你6,第二个会给你8 ... –
- 1. 如何从文本中提取数字?
- 2. 从字母数字字符串中提取数字
- 3. 使用RegEx从字母数字字符串中提取数字
- 4. 提取字母数字值
- 5. 从字母数字QString中提取数字
- 6. 通过使用VB脚本从字母数字中提取数字
- 7. 如何从文本文件中提取数字数据?
- 8. 从字段数组中提取文本
- 9. 从文本框中提取数字
- 10. 从文本框中提取数字
- 11. 从字符串中提取文本,连字符和数字
- 12. 从一串数字和文本中提取数字
- 13. 从文本文件中查找字母数字字符串
- 14. 从字母数字字段中提取6-8位数字的日期
- 15. 在Swift中从文本文件中提取数字和字符
- 16. 如何从字符串中获取数字和字母?
- 17. 使用数字和字母在字符串中提取整数
- 18. 如何从数组中提取数字
- 19. 从数字中提取个人数字。
- 20. 从文件中提取字母
- 21. 如何从文本文件中一次提取100个数字?
- 22. Vimperator字母数字提示
- 23. 从图像中提取数字和字母特征
- 24. 如何从SQL中的文本中提取数字范围
- 25. 如何从字符中提取MySQL数据库中的文本?
- 26. 提取文本文件中的数字
- 27. 从一个文本字符串中提取数字 - Excel
- 28. 从“/”之间的字符串中提取文本和数字
- 29. 如何为数字文本字段和字母文本字段设置键盘?
- 30. 从javascript中的字母数字字符串获取数值
,因为我的手比较痒我写了一个代码参照我从评论中提到的功能。所以没有交叉发布。但要让您了解如何读取Excel单元格范围以处理文本然后输出。希望让你去;) – bonCodigo
当你可以使用正则表达式时,不需要太多的代码 – InContext
如果你的数字总是在字符串的前面,那么正则表达式是矫枉过正的。 – brettdj