2楼 棉花糖 |
编了一个vba,同样实现效果。
- Sub replacespecialcharacters()
- Dim rng As Range, cell As Range
- Dim Reptxt
- Dim FirstAddress As String
- Dim TxtLen, i
-
- '选择查找的范围和替换的内容,不符合要求就退出
- On Error Resume Next
- Set rng = Application.InputBox(prompt:="选取替换的范围", Title:="替换", Type:=8)
- If rng Is Nothing Then Exit Sub
- Reptxt = InputBox(prompt:="输入要替换的字符", Title:="替换")
- If Reptxt = "" Then Exit Sub
- On Error GoTo 0
-
- TxtLen = Len(Reptxt)
- i = 1
-
- '查找到第一个符合条件的单元格,没有就退出
- Set cell = rng.Find(What:=Reptxt, _
- After:=rng.Range("a1").Offset(rng.Rows.Count - 1, rng.Columns.Count - 1), _
- LookIn:=xlValues, _
- LookAt:=xlPart, _
- SearchOrder:=xlByRows, _
- SearchDirection:=xlNext, _
- MatchCase:=False)
- If Not cell Is Nothing Then
- FirstAddress = cell.Address
- Else
- Exit Sub
- End If
-
- Do '查找每个符号单元格
- Do '查找单元格内替换字符内容次数,逐个替换
- cell.Characters(Start:=InStr(i, cell.Value, Reptxt, vbTextCompare), Length:=TxtLen). _
- Font.Color = RGB(255, 0, 0)
- i = InStr(InStr(i, cell.Value, Reptxt, vbTextCompare) + TxtLen + 1, _
- cell.Value, Reptxt, vbTextCompare)
- Loop While i <> 0
- i = 1
- Set cell = rng.FindNext(cell)
- Loop While cell.Address <> FirstAddress '避免查找死循环
- End Sub
附件:
如何批量设置数据中关键字的字体颜色?.rar |