2014-06-23 160 views
3

我使用VBA读取一些标题,然后将该信息复制到PowerPoint演示文稿。删除特殊字符VBA Excel

我的问题是,TITLES有特殊字符,但我也在应对的图像文件没有。

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

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

这可能吗?

谢谢!

回答

18

你认为什么是“特殊”字符,只是简单的标点符号?你应该可以使用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 
+0

伟大的职位。但它不适合角色? – JohnAndrews

+1

@JohnAndrews是的,如果你修改'SpecialCharacters'字符串,以便问号是其中的一部分:'Const SpecialCharacters As String =“!,@,#,$,%,^,&,*,(,) {,[,],},?“'。 –

3

在你不仅要排除的特殊字符列表,但要排除并非字母或数字所有角色,我会建议你使用一个char类型比较法的情况。

对于字符串中的每个字符,我会检查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是一个很好的快速启动,如果你想定制这个功能多一点。

即使用户找到引入新的特殊字符的方法,该解决方案的优势在于功能。它也比将两个列表比较在一起更快。

1

这是我使用,在此基础上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 
4

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

我直接应用正则表达式

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)