我正在尝试删除单元格中的所有字母,并保留剩余的数字。 我在这里找到了一些代码和其他问题,但没有一个对我有意义。
我在单元格E23" as12df34"并希望Cell E23的值读取" 12 34"
有人可以帮忙解决这个问题吗?
答案 0 :(得分:2)
您可以使用正则表达式:
Sub UsageExample()
Dim cl
' iterate each cell
For Each cl in Range("Sheet1!A1:A100")
' replace each non digit sequence by a space
cl.Value = ReplaceRe(cl.Value, "\D+", " ")
Next
End Sub
Public Function ReplaceRe(text As String, pattern As String, replacement) As String
Static re As Object
If re Is Nothing Then
Set re = CreateObject("VBScript.RegExp")
re.Global = True
End If
re.pattern = pattern
ReplaceRe = re.Replace(text, replacement)
End Function
答案 1 :(得分:0)
如果你想做类似的事情,这是一个UDF。将“Spaces”设为True或False将允许您拥有一个单独的空格,其中包含非数字字符。
Sub Test()
Debug.Print Nums("as12df34", True)
End Sub
Function Nums(What As String, Spaces As Boolean) As String
Dim i As Long
For i = 1 To Len(What)
If IsNumeric(Mid(What, i, 1)) = True Then Nums = Nums & Mid(What, i, 1)
If IsNumeric(Mid(What, i, 1)) = False Then Nums = Nums & " "
Next i
Nums = Trim(Nums)
If Spaces = True Then
Do Until InStr(Nums, " ") = 0
Nums = Replace(Nums, " ", " ")
Loop
Else
Do Until InStr(Nums, " ") = 0
Nums = Replace(Nums, " ", "")
Loop
End If
End Function
答案 2 :(得分:0)
我知道这可能已经得到了解答,但我想让其他可能遇到这个问题的人看到另一种可能性。我想出了一个明显的解决方案来消除所有要替换的字母,只留下单元格中的数字。你可以直接替换""为了" "离开字母留下的空间。
这是一个巨大的混乱,但我使用它并按预期工作只需将功能拖到下一个单元格。无需打字。在我的情况下,我有一个像"白金锭,3"它将删除所有可用于计算内容的字母,逗号和空格以及3。当1的值永远不包含数字时,我用它来在1个单元格中保存2个值。
= SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE( SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(替换(替换(替换(替换(替换(替换(F5," A","")," B","" )" C""")," d""")," E",& #34;")," F""")," G"""),&#34 ; H""")," I""")," J""&# 34)" K""")," L""")," M" "")," N""")," O"""),& #34; P""")," Q&#3 4;,"")," R""")," S""&#34) " T""")," U""")," V"&# 34;")," W""")," X""")," Y'#34;"")," Z"""),"""&#34 ;)," b""")," C""")," d&#34 ;, "")," E""")," F"""),&# 34; G""")," H""")," I""& #34;)," J""")," K""")," L&#34 ;,"")," M""")," N""&#34), " O""")," p""")," q"&#34 ;")," R""")," S""")," T& #34;,"")," u","")," v","" )," w","")," x","")," y",& #34;")," Z"""),""""),&# 34; ""&#34)