|
用VBA清理干扰字符:
首先,要手工完成一项准备工作:选中一个需要清理干扰字符的单元格,把它的字符串开始位置的连续干扰字符都删除,直到第一个需要保留的字符之前为止。这个单元格将用来做为需要保留字符的样本单元格;
然后,选中所有需要清理干扰字符的单元格区域,运行以下宏代码,按提示选择上一步预备的保留字符样本单元格,点确定,代码将会自动完成对干扰字符的清理。
- Sub CleanConfusionCharacters()
- Dim rng0 As Range, rng1 As Range
- Dim cel As Range, ar As Range, s As String
- Dim i As Long, sz As Single, cl As Double, k As Long
- On Error Resume Next
- Set rng0 = ActiveWindow.RangeSelection.SpecialCells(xlCellTypeConstants)
- If rng0 Is Nothing Then Exit Sub
- Set rng1 = Application.InputBox("请选择保留字符的样本单元格", "清理混淆字符", ActiveCell.Address(0, 0), Type:=8)
- If rng1 Is Nothing Then Exit Sub
- sz = rng1.Characters(1, 1).Font.Size
- cl = rng1.Characters(1, 1).Font.Color
- If MsgBox("保留字符:" & sz & "号,字体RGB颜色值:" & cl & vbLf & "是否继续?", vbYesNo) = vbNo Then Exit Sub
- Application.ScreenUpdating = False
- For Each ar In rng0.Areas
- For Each cel In ar
- s = Empty
- For i = Len(cel.Text) To 1 Step -1
- With cel.Characters(i, 1)
- If .Font.Size <> sz Or .Font.Color <> cl Then .Text = ""
- End With
- Next
- cel.Font.Size = sz
- cel.Font.Color = cl
- k = k + 1
- If k = 100 Then k = 0: DoEvents
- Next
- Next
- End Sub
复制代码 |
|