我正在修改我编写的用户定义函数。它从单元格中删除特殊字符(我已经发布了几次相同的功能,因为我不断扩展它并更多地了解 VBA 的功能)。
我现在要做的是添加一个 MsgBox,它会弹出并准确地告诉用户哪些特殊字符已被删除。我认为我可以通过使用嵌套在现有 for 循环中的 If 语句来做到这一点,如下所示:
Function removeSpecial(sInput As String) As String
Dim sSpecialChars As String
Dim i As Long
Dim sReplaced As String
sSpecialChars = "\/:*?™""®<>|.&@# %(_+`©~);-+=^$!,'" 'This is your list of characters to be removed
For i = 1 To Len(sSpecialChars)
sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
If (sInput = sSpecialChars) Then 'If statement to check when a character has been removed
sReplaced = sInput 'String variable to store each special character which was replaced
Next
MsgBox sReplaced & " These were removed"
sInput = UCase(sInput)
removeSpecial = sInput
End Function 'end of function
目前这让我陷入了一个无限循环,我必须强制关闭 Excel。我试图用上面的代码做的是检查一个单独的字符是否位于 Mid 函数当前正在查看的任何索引处,然后将该字符(如果被替换)保存到 String sReplaced。很明显,我在我的头上。
谢谢你的帮助。