简体   繁体   English

从单元格中删除字符

[英]Remove characters from a cell

This formula removes everything except numbers: 此公式将除去数字以外的所有内容:

Sub qwerty()
    For Each r In Selection
        vout = ""
        v = r.Text
        n = Len(v)
        For i = 1 To n
            ch = Mid(v, i, 1)
            If ch Like "[0-9]" Then
                vout = vout & ch
            End If
        Next i
        r.Value = vout
    Next r
End Sub  

How can I modify it so that it will keep numbers and decimal points? 如何修改它以便保留数字和小数点?

To include . 包括在内. , include it in your Like expression ,将其包含在Like表达式中

If ch Like "[0-9.]" Then

Better to use a Regexp than slowly loop character by character through an inital range loop: 最好使用Regexp不是通过一个初始范围循环逐个字符地循环循环:

Sub Quicker()
Dim X
Dim ObjRegex As Object
Dim lngRow As Long
Dim lngCOl As Long

Set ObjRegex = CreateObject("vbscript.regexp")
ObjRegex.Global = True
ObjRegex.Pattern = "[^0-9/.]"

X = Selection.Value2
For lngRow = 1 To UBound(X, 1)
   For lngCOl = 1 To UBound(X, 2)
        X(lngRow, lngCOl) = ObjRegex.Replace(X(lngRow, lngCOl), vbNullString)
   Next
Next

Selection.Value2 = X

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM