2017-04-03 133 views
2

我正在使用此功能刪除所有假定的特殊字符,但仍然很少有特殊字符,例如「Ø」或「é」,並且所有類似字符仍然存在如何在下面給出的函數中處理此問題。刪除Excel中的特殊字符

我的想法是,創建一個像MyStr這樣的特殊字符的字符串並將其刪除。

請建議。

Function ReplaceSplChars(TempStr As String) As String 

Dim counter As Long, Position As Long 
Dim MyStr As String, SplStr As String 


MyStr = " [email protected]" 
Position = 1 

For counter = 1 To Len(TempStr) 
    SplStr = Mid(LCase(TempStr), Position, 1) 

    If Not InStr(MyStr, SplStr) > 0 Then 

     If SplStr = "-" Or SplStr = "_" Or SplStr = "'" Or SplStr = "/" Or SplStr = "," Then 
      TempStr = Replace(TempStr, SplStr, " ", , 1) 
      SplCharCount = SplCharCount + 1 
      Position = Position + 1 
     Else 
      TempStr = Replace(TempStr, SplStr, "", , 1) 
      SplCharCount = SplCharCount + 1 
     End If 
    Else 
     Position = Position + 1 
    End If 

Next counter 
ReplaceSplChars = TempStr 
End Function 

回答

2

A 需要努力工作。

測試下面的代碼

Sub EddieBetts() 
MsgBox ReplaceSplChars(" 12345Ø67890abcdefghijklmnopqrsté[email protected]") 
End Sub 

模式排除任何是

  • 字母數字(\ W
  • 空間(\ S
  • @

代碼

Function ReplaceSplChars(strIn As String) As String 
Dim objRegex As Object 
    Set objRegex = CreateObject("vbscript.regexp") 
    With objRegex 
     .Pattern = "[^\w\[email protected]]+" 
     .Global = True 
    ReplaceSplChars = Application.Trim(.Replace(strIn, vbNullString)) 
    End With 
End Function 
+0

尼斯使用正則表達式。但請注意,字符類括號是多餘的。 –

+0

是的。開始一場負面比賽並沒有完全放鬆。 – brettdj

+0

你好,謝謝你的回覆。 –