如何从字符串中查找数字?我需要从a中找到数字string。如何从stringVBA Excel中查找数字?
3 回答
梵蒂冈之花
TA贡献1900条经验 获得超5个赞
构造正则表达式来解析。虽然语法可能需要一段时间才能掌握这种方法非常有效,并且非常灵活,可以处理更复杂的字符串提取/替换
Sub Tester() MsgBox CleanString("3d1fgd4g1dg5d9gdg")End SubFunction CleanString(strIn As String) As String Dim objRegex Set objRegex = CreateObject("vbscript.regexp") With objRegex .Global = True .Pattern = "[^\d]+" CleanString = .Replace(strIn, vbNullString) End WithEnd Function
有只小跳蛙
TA贡献1824条经验 获得超8个赞
扩展brettdj的答案,以便将不相交的嵌入数字解析为单独的数字:
Sub TestNumList() Dim NumList As Variant 'Array NumList = GetNums("34d1fgd43g1 dg5d999gdg2076") Dim i As Integer For i = LBound(NumList) To UBound(NumList) MsgBox i + 1 & ": " & NumList(i) Next iEnd SubFunction GetNums(ByVal strIn As String) As Variant 'Array of numeric strings Dim RegExpObj As Object Dim NumStr As String Set RegExpObj = CreateObject("vbscript.regexp") With RegExpObj .Global = True .Pattern = "[^\d]+" NumStr = .Replace(strIn, " ") End With GetNums = Split(Trim(NumStr), " ")End Function
添加回答
举报
0/150
提交
取消