删除特殊字符VBA Excel

时间:2014-06-23 00:23:15

标签: excel vba excel-vba excel-2010

我正在使用VBA读取一些TITLES,然后将该信息复制到powerpoint演示文稿中。

我的问题是,TITLES有特殊字符,但我也要处理的图像文件没有。

TITLE构成了将JPEG加载到图片容器中的路径的一部分。例如。 “P k.jpg”,但标题被称为“p.k”。

我希望能够忽略TITLE中的特殊字符,只是让它看到一个空格,以便它选择正确的JPG文件。

这可能吗?

谢谢!

4 个答案:

答案 0 :(得分:31)

你认为什么"特别"字符,只是简单的标点符号?您应该能够使用Replace函数:Replace("p.k","."," ")

Sub Test()
Dim myString as String
Dim newString as String

myString = "p.k"

newString = replace(myString, ".", " ")

MsgBox newString

End Sub

如果您有多个字符,可以在自定义函数或Replace函数的简单链接系列等中执行此操作。

  Sub Test()
Dim myString as String
Dim newString as String

myString = "!p.k"

newString = Replace(Replace(myString, ".", " "), "!", " ")

'## OR, if it is easier for you to interpret, you can do two sequential statements:
'newString = replace(myString, ".", " ")
'newString = replace(newString, "!", " ")

MsgBox newString

End Sub

如果您有很多潜在的特殊字符(例如非英语口音的ascii?),您可以对数组执行自定义函数或迭代。

Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?"  'modify as needed
Sub test()
Dim myString as String
Dim newString as String
Dim char as Variant
myString = "!p#*@)k{kdfhouef3829J"
newString = myString
For each char in Split(SpecialCharacters, ",")
    newString = Replace(newString, char, " ")
Next
End Sub

答案 1 :(得分:7)

如果您不仅要排除特殊字符列表,而且要排除非字母或数字的所有字符,我建议您使用字符类型比较方法。

对于String中的每个字符,我将检查unicode字符是否在“A”和“Z”之间,“a”和“z”之间或“0”和“9”之间。这是vba代码:

Function cleanString(text As String) As String
    Dim output As String
    Dim c 'since char type does not exist in vba, we have to use variant type.
    For i = 1 To Len(text)
        c = Mid(text, i, 1) 'Select the character at the i position
        If (c >= "a" And c <= "z") Or (c >= "0" And c <= "9") Or (c >= "A" And c <= "Z") Then
            output = output & c 'add the character to your output.
        Else
            output = output & " " 'add the replacement character (space) to your output
        End If
    Next
    cleanString = output
End Function

Wikipedia list of Unicode characers是一个很好的快速启动,如果你想再多一点定制这个功能。

即使用户找到引入新特殊字符的方法,该解决方案也具有功能的优点。它也比将两个列表比较快。

答案 2 :(得分:7)

以下是删除特殊字符的方法。

我只是应用了正则表达式

Dim strPattern As String: strPattern = "[^a-zA-Z0-9]" 'The regex pattern to find special characters
Dim strReplace As String: strReplace = "" 'The replacement for the special characters
Set regEx = CreateObject("vbscript.regexp") 'Initialize the regex object    
Dim GCID As String: GCID = "Text #N/A" 'The text to be stripped of special characters

' Configure the regex object
With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = False
    .Pattern = strPattern
End With

' Perform the regex replacement
GCID = regEx.Replace(GCID, strReplace)

答案 3 :(得分:2)

这是我使用的,基于此link

Function StripAccentb(RA As Range)

Dim A As String * 1
Dim B As String * 1
Dim i As Integer
Dim S As String
'Const AccChars = "ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
'Const RegChars = "SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
Const AccChars = "ñéúãíçóêôöá" ' using less characters is faster
Const RegChars = "neuaicoeooa"
S = RA.Cells.Text
For i = 1 To Len(AccChars)
A = Mid(AccChars, i, 1)
B = Mid(RegChars, i, 1)
S = Replace(S, A, B)
'Debug.Print (S)
Next


StripAccentb = S

Exit Function
End Function

用法:

=StripAccentb(B2) ' cell address

工作表中所有单元格的子版本:

Sub replacesub()
Dim A As String * 1
Dim B As String * 1
Dim i As Integer
Dim S As String
Const AccChars = "ñéúãíçóêôöá" ' using less characters is faster
Const RegChars = "neuaicoeooa"
Range("A1").Resize(Cells.Find(what:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
Cells.Find(what:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column).Select '
For Each cell In Selection
If cell <> "" Then
S = cell.Text
    For i = 1 To Len(AccChars)
    A = Mid(AccChars, i, 1)
    B = Mid(RegChars, i, 1)
    S = replace(S, A, B)
    Next
cell.Value = S
Debug.Print "celltext "; (cell.Text)
End If
Next cell
End Sub