如何删除字符?

时间:2011-09-12 06:25:19

标签: excel vba excel-vba excel-formula

如何删除字符串中的特殊字符和字母?

 qwert1234*90)!  ' this might be my cell value

我必须将其转换为

 123490  ' I mean I have to remove everything but keep only the numbers in string

但它应该允许空格!

 qwe123 4567*. 90  ' String with spaces
 123 4567 90     ' output should be

我找到了 Replace - 但是为每个字符编写替代品会使我的代码变大。好吧,让我清楚地告诉你,不要向你隐瞒任何事情:

  1. input:qwe123 4567*. 90'带空格的字符串(1,“A”)。value
  2. 我接下来要做的是这样做:123 4567 90'首先删除保留空格的字符
  3. A1:A3

    中的最终输出
      

    123
       4567
       90

  4.      (对于每个空格,它应插入行并填充)

    你能告诉我如何删除字符串中除数字和空格以外的所有字符吗?

    提前致谢

4 个答案:

答案 0 :(得分:12)

您需要使用正则表达式。

见这个例子:

Option Explicit

Sub Test()
    Const strTest As String = "qwerty123 456 uiops"
    MsgBox RE6(strTest)
End Sub

Function RE6(strData As String) As String
    Dim RE As Object, REMatches As Object

    Set RE = CreateObject("vbscript.regexp")
    With RE
        .MultiLine = False
        .Global = True
        .IgnoreCase = True
        .Pattern = "([0-9]| )+"   
    End With

    Set REMatches = RE.Execute(strData)
    RE6 = REMatches(0)

End Function

<强>解释
Pattern = "([0-9]| )+"将匹配包含数字(+)或([0-9])空格(|)的任意0个或更多组()。

正则表达式

更多信息

答案 1 :(得分:9)

非替代品;

Public Function fmt(sValue As String) As String
    Dim i As Long
    For i = 1 To Len(sValue) '//loop each char
        Select Case Mid$(sValue, i, 1) '//examine current char
            Case "0" To "9", " " '//permitted chars
               '//ok
            Case Else
               Mid$(sValue, i, 1) = "!" '//overwrite char in-place with "!"
        End Select
    Next
    fmt = Replace$(sValue, "!", "") '//strip invalids & return
End Function

有关:

?fmt("qwe123 4567*. 90") 
123 4567 90

答案 2 :(得分:4)

这两个有趣的代码可以完成你的两件事......

Sub MySplitter(strInput As String)
    Row = 10  ' Start row
    Col = "A" ' Column Letter
    Range(Col & Row) = ""   ' Clean the start cell
    For i = 1 To Len(strInput)  ' Do with each Character in input string...
        c = Mid(strInput, i, 1) ' Get actual char
        If IsNumeric(c) Then Range(Col & Row) = Range(Col & Row) & c ' If numeric then append to actual cell
        If (c = " ") And (Range(Col & Row) <> "") Then 'If space and actual row is not empty then...
            Row = Row + 1           ' Jump to next row
            Range(Col & Row) = ""   ' Clean the new cell
        End If
    Next
End Sub

Function KeepNumbersAndSpaces(ByVal strInput As String)
    For i = 1 To Len(strInput)  ' Do with each Character in input string...
        c = Mid(strInput, i, 1) ' Get actual char
        If IsNumeric(c) Or c = " " Then ' If numeric or a space then append to output
            KeepNumbersAndSpaces = KeepNumbersAndSpaces & c
        End If
    Next
End Function

Sub Test()
    strInput = "qwert1234*90)! qwe123 4567*. 90"
    MySplitter (strInput)
    Range("A5") = KeepNumbersAndSpaces(strInput)
End Sub

答案 3 :(得分:2)

这样的事情

  • 使用
  • 拆分字符串
  • 将匹配项放入数组
  • 将数组转储到自动调整大小的电子表格范围

主要

Sub CleanStr()
Dim strOut As String
Dim Arr
strOut = Trim(KillChar("qwe123 4567*. 90 "))
Arr = Split(strOut, Chr(32))
[a1].Resize(UBound(Arr) + 1, 1) = Application.Transpose(Arr)
End Sub

功能

Function KillChar(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
        .Global = True
        .Pattern = "[^\d\s]+"
        KillChar = .Replace(strIn, vbNullString)
    End With
End Function