您好,江生 张
欢迎在微软社区发帖提问。
如果需要从指定单元格提取数字,而忽略其它字符,可以编写一个通过正则表达式匹配数字的自定义函数。
打开Excel并按 Alt + F11 进入VBA编辑器。
在VBA编辑器中,点击 插入 > 模块,然后在模块窗口中粘贴以下代码: Function ExtractNumbers(cell As Range) As String
Dim regex As Object
Dim matches As Object
Dim match As Object
Dim result As String
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "\d+"
regex.Global = True
Set matches = regex.Execute(cell.Value)
result = ""
For Each match In matches
result = result & match.Value
Next match
ExtractNumbers = result
End Function
保存并关闭VBA编辑器。
回到Excel工作表中,您可以使用自定义函数 ExtractNumbers。例如,在单元格A1中输入包含数字和其他字符的文本,然后在单元格B1中输入公式 =ExtractNumbers(A1),结果将是提取出的数字。
您可以随时与我分享您尝试的结果。如果您在此过程遇到任何问题,请随时与我联系。
此致敬意,
Jonathan Z | Microsoft 社区支持专员