删除特殊字符VBA Excel

我使用VBA来阅读一些标题,然后将这些信息复制到PowerPoint演示文稿。

我的问题是,名人有特殊的字符,但我也应付的图像文件不。

TITLE形成将JPEG加载到图片容器的path的一部分。 例如“P k.jpg”,但标题被称为“pk”。

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

那可能吗?

谢谢!

你认为什么是“特殊”字符,只是简单的标点符号? 您应该能够使用Replacefunction: Replace("pk","."," ")

 Sub Test() Dim myString as String Dim newString as String myString = "pk" newString = replace(myString, ".", " ") MsgBox newString End Sub 

如果你有几个字符,你可以在一个自定义函数或简单的一系列Replace函数等中做到这一点。

  Sub Test() Dim myString as String Dim newString as String myString = "!pk" 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 

这里是如何删除特殊字符。

我只是申请正则expression式

 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) 

在不仅要排除特殊字符列表,而且要排除不是字母或数字的所有字符的情况下,我build议您使用chartypes比较方法。

对于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 

如果你想自定义这个function, 维基百科的Unicodestring列表是一个很好的快速入门 。

即使用户find引入新的特殊字符的方法,该解决scheme也具有function性的优点。 它也比两个列表比较快。

这就是我所使用的,基于这个链接

 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