从输入框中删除所有文本?

时间:2016-11-07 12:52:05

标签: vba excel-vba excel

如何从输入框中删除所有字符,只留下数字?

我有一个宏在列中删除空白区域,缩短为13位但我还需要删除任何文本字符。

3 个答案:

答案 0 :(得分:0)

使用regular expression的另一种方法是:

Public Sub removeCharacters()

    For Each RANGE_UNASSIGNED In Worksheets(1).Range("A1:A" & Worksheets(1).Range("A1").End(xlDown).Row)
    STRING_OUTPUT = ""
        For INTEGER_STEP = 1 To Len(RANGE_UNASSIGNED.Value)
            STRING_TEMPORARY = Mid(RANGE_UNASSIGNED.Value, INTEGER_STEP, 1)
                If STRING_TEMPORARY Like "[a-z.]" Or STRING_TEMPORARY Like "[A-Z.]" Then
                    STRING_xOUTPUT = ""
                Else
                    STRING_xOUTPUT = STRING_TEMPORARY
                End If
                STRING_OUTPUT = STRING_OUTPUT & STRING_xOUTPUT
        Next INTEGER_STEP
        RANGE_UNASSIGNED.Value = STRING_OUTPUT
    Next RANGE_UNASSIGNED
End Sub

这应该从您的单元格中删除所有字母字符。如果需要,您可以删除其他字符。

答案 1 :(得分:0)

基于IsNumeric的方法。

Sub Keep_If_IsNumeric()

For j = 1 To Cells(Rows.Count, "A").End(xlUp).Row

Set c = Cells(j, 1)

strc = ""

For i = 1 To Len(c.Value)

n = Mid(c.Value, i, 1)

 If Not IsNumeric(strc & n & "0") Then

   Else

   strc = strc & n

 End If

Next
c.Offset(, 1) = strc
c.Offset(, 2) = Val(Replace(strc, ",", "."))

Next
End Sub

答案 2 :(得分:0)

我仍然认为regexp是可行的方法。

Function removeAlpha(strData As String) As String

    strData = Replace(strData, " ", "")

    With CreateObject("vbscript.regexp")
        .Pattern = "[A-Za-z]"
        .Global = True
         removeAlpha = .Replace(strData, "")
    End With

End Function

并测试:

Sub TestClean()
    Const strTest As String = "qwerty123 456 uiops"
    MsgBox removeAlpha(strTest)
End Sub