vba 从具有不同颜色文本的 Excel 电子表格中仅删除特定颜色的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12042161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete text only of a specific color from an Excel spreadsheet with text in different colors
提问by user1612370
I have a spreadsheet with text in two colors, often in the same cells. I would like to run a macro which deletes only the black text from each cell in which it appears.
我有一个包含两种颜色文本的电子表格,通常在同一个单元格中。我想运行一个宏,它只从出现的每个单元格中删除黑色文本。
回答by SteveeP
If you don't want to use a macro, why not use the "find and select" tool. Go to "replace". select "options". Select "format" and select the font colour or cell colour option. choose the colour you want to get rid of, leave the "replace with" box blank and click "replace all"
如果您不想使用宏,为什么不使用“查找和选择”工具。转到“替换”。选择“选项”。选择“格式”并选择字体颜色或单元格颜色选项。选择您要删除的颜色,将“替换为”框留空,然后单击“全部替换”
回答by Stepan1010
This should work for you:
这应该适合你:
Sub ForEachCharacterTextColor()
Dim wbk As Workbook
Dim i As Integer
Set wbk = ThisWorkbook
Set ws = wbk.Sheets(1)
Dim cell As Range
Cells.Select
Selection.NumberFormat = "@"
Range("A1").Select
On Error GoTo MyExitSub
With ws
For Each cell In ws.Range("A1:E2000").Cells
'cell.Value = "'" & cell.Value
For i = 1 To Len(cell)
If cell.Characters(i, 1).Font.Color = RGB(0, 0, 0) Then
If Len(cell) > 0 Then
cell.Characters(i, 1).Delete
End If
If Len(cell) > 0 Then
i = i - 1
End If
End If
Next i
Next cell
End With
MyExitSub:
If Err.Number > 0 Then
MsgBox "Some of your cells are numbers formatted as text. You need to convert them to text completely."
End
End If
End Sub
Good Luck. - It will work as long as excel doesn't think any of your cells are intended to be numbers.
祝你好运。- 只要 excel 不认为您的任何单元格都是数字,它就会起作用。