|
Sub 删除非法字符()
Application.Calculation = xlCalculationManual '手动重算
On Error Resume Next '忽略错误继续执行VBA代码,避免出现错误消息
For Each r In ActiveSheet.UsedRange '数组方式——将当前工作表所有单元纳入数组
a = r.Row
b = r.Column
If Cells(a, b) <> Application.WorksheetFunction.Substitute(Cells(a, b), ChrB(160), "") _
Or Cells(a, b) <> Application.WorksheetFunction.Substitute(Cells(a, b), Chr(9), "") Then '判断单元是否包含非法字符
Cells(a, b) = Application.WorksheetFunction.Substitute(Cells(a, b), ChrB(160), "") '替换特殊字符
Cells(a, b) = Application.WorksheetFunction.Substitute(Cells(a, b), Chr(9), "") '替换特殊字符
Cells(a, b).Interior.ColorIndex = 35 '单元颜色浅绿
End If
Next
On Error GoTo 0 '恢复正常的错误提示
Application.Calculation = xlCalculationAutomatic '自动重算
End Sub |
|