我如何删除angular色?

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

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 

我发现vba Replace – 但是为每个字符写一个replace使我的代码大。 那么让我告诉你,没有隐藏任何东西,

  1. input: qwe123 4567*. 90 qwe123 4567*. 90 '带空格的string(1,“A”)
  2. 我的想法接下来做: 123 4567 90 '先删除字符保持空格
  3. A1:A3最终输出

123
4567
90 (对于每个空间,它应该插入行和填充)

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

提前致谢

你需要使用正则expression式。

看到这个例子:

 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个或多个组( + )。

有关正则expression式的更多信息

  • ozgrid上的线程
  • 关于正则expression式的一个非常好的参考

不可替代;

 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 

这两个有趣的代码将做你的whishes ..

 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 

像这样的东西

  • 使用正则expression式分割string
  • 将匹配放入数组中
  • 将数组转储到自动调整的电子表格范围

主要分

 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

 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