我使用VBA讀取一些標題,然後將該信息複製到PowerPoint演示文稿。刪除特殊字符VBA Excel
我的問題是,TITLES有特殊字符,但我也在應對的圖像文件沒有。
TITLE構成將JPEG加載到圖片容器的路徑的一部分。例如。 「P k.jpg」,但標題被稱爲「p.k」。
我想能夠忽略TITLE中的特殊字符,只是讓它看到一個空格,所以它會選擇正確的JPG文件。
這可能嗎?
謝謝!
我使用VBA讀取一些標題,然後將該信息複製到PowerPoint演示文稿。刪除特殊字符VBA Excel
我的問題是,TITLES有特殊字符,但我也在應對的圖像文件沒有。
TITLE構成將JPEG加載到圖片容器的路徑的一部分。例如。 「P k.jpg」,但標題被稱爲「p.k」。
我想能夠忽略TITLE中的特殊字符,只是讓它看到一個空格,所以它會選擇正確的JPG文件。
這可能嗎?
謝謝!
你認爲什麼是「特殊」字符,只是簡單的標點符號?你應該可以使用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
在你不僅要排除的特殊字符列表,但要排除並非字母或數字所有角色,我會建議你使用一個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是一個很好的快速啓動,如果你想定製這個功能多一點。
即使用戶找到引入新的特殊字符的方法,該解決方案的優勢在於功能。它也比將兩個列表比較在一起更快。
這是我使用,在此基礎上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
這裏是如何去除特殊字符。
我直接應用正則表達式
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)
偉大的職位。但它不適合角色? – JohnAndrews
@JohnAndrews是的,如果你修改'SpecialCharacters'字符串,以便問號是其中的一部分:'Const SpecialCharacters As String =「!,@,#,$,%,^,&,*,(,) {,[,],},?「'。 –