删除excel vba中的特殊字符

时间:2017-04-03 07:29:00

标签: excel vba

我正在使用此功能删除所有假想的特殊字符,但仍然很少有特殊字符,如“Ø”或“é”,所有相似的字符仍然存在如何处理这个也在下面的给定功能。

我的想法是创建一些像MyStr这样的特殊字符的字符串并删除它们。

请建议。

 Function ReplaceSplChars(TempStr As String) As String

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


MyStr = " 1234567890abcdefghijklmnopqrstuvwxyz.@"
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

1 个答案:

答案 0 :(得分:2)

需要努力工作。

测试代码

Sub EddieBetts()
MsgBox ReplaceSplChars(" 12345Ø67890abcdefghijklmnopqrstéuvwxyz.@")
End Sub

下面的模式排除任何

的内容
  • 字母数字( \ w
  • 空格( \ s
  • @

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