我正在清理一大堆充满符号字符的数据。 !@%^&*|'~<>?·
并希望一次删除它们。下面的脚本似乎有效地这样做,然后它继续删除工作簿的全部内容。
Sub Removeforeigncharacters()
Dim ForeignChars As String
Dim RegChars As String
ForeignChars = "!@%^&*|`~<>?·"
RegChars = ""
Set MyRange = ActiveSheet.UsedRange
Dim A As String * 1
Dim B As String * 1
Dim i As Integer
For i = 1 To Len(ForeignChars)
A = Mid(ForeignChars, i, 1)
B = Mid(RegChars, i, 1)
MyRange.Replace What:=A, Replacement:=B, LookAt:=xlPart, MatchCase:=False
Next
End Sub
答案 0 :(得分:5)
What
Range.Replace
参数使用*
作为通配符,这意味着它将匹配每个字符(并替换它们)。请注意,这在技术上并不是删除字符,因为你使用了固定长度的字符串 - 它用空格替换它们,因为代码......
B = Mid(RegChars, i, 1)
...总是会产生空间。
如果您需要搜索文字*
而不是通配符匹配,则需要转义它们:
Dim A As String
Dim i As Integer
For i = 1 To Len(ForeignChars)
A = Mid(ForeignChars, i, 1)
If A Like "[~*?`]" Then A = "~" & A
MyRange.Replace What:=A, Replacement:=vbNullString, LookAt:=xlPart, MatchCase:=False
Next
(我可能错过了一些需要逃脱的事情。)
请注意,如果您这样做,则不能将A
声明为固定长度的字符串(无论如何,确实没有令人信服的理由)。
答案 1 :(得分:2)
使用数组可以显着提升性能。
以下是:
Sub Removeforeigncharacters()
Dim x As Long, y As Long
Dim Data As Variant, ForeignChars As Variant, v As Variant
ForeignChars = "!@%^&*|`~<>?·"
Data = ActiveSheet.UsedRange.Value2
For x = 1 To UBound(Data, 1)
For y = 1 To UBound(Data, y)
For Each v In ForeignChars
If InStr(Data(x, y), v) Then
Data(x, y) = Replace(Data(x, y), v, "")
End If
Next
Next
Next
ActiveSheet.UsedRange.Value2 = Data
End Sub
答案 2 :(得分:1)
您必须逃避您的问题:
Sub Removeforeigncharacters()
Dim ForeignChars As String
Dim RegChars As String
ForeignChars = "!@%^&|`~<>·"
RegChars = ""
Set MyRange = ActiveSheet.UsedRange
Dim A As String * 1
Dim B As String * 1
Dim i As Integer
For i = 1 To Len(ForeignChars)
A = Mid(ForeignChars, i, 1)
B = Mid(RegChars, i, 1)
MyRange.Replace What:=A, Replacement:=B, LookAt:=xlPart, MatchCase:=False
Next
MyRange.Replace What:="~*", Replacement:="", LookAt:=xlPart, MatchCase:=False
MyRange.Replace What:="~`", Replacement:="", LookAt:=xlPart, MatchCase:=False
MyRange.Replace What:="~?", Replacement:="", LookAt:=xlPart, MatchCase:=False
MyRange.Replace What:="~~", Replacement:="", LookAt:=xlPart, MatchCase:=False
End Sub
注意我减少了ForeignChars