vba 循环遍历工作簿中所有工作表中的所有单元格,如果为红色则更改格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5820167/
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
loop through all cells in all sheets in workbook, change format if red
提问by jakek04
I'm trying to change all red text to black in all cells for all sheets of an excel workbook. I'm using the following:
我正在尝试将 Excel 工作簿的所有工作表的所有单元格中的所有红色文本更改为黑色。我正在使用以下内容:
Sub RedToBlack()
Dim Current As Worksheet
For Each Current In Worksheets
For Each cell In Current
If cell.Font.ColorIndex = 3 Then
cell.Font.ColorIndex = 0
End If
Next
Next
End Sub
I know this is wrong, but wanted to give a sense of what I'm trying to do. Can anyone offer advice? Thanks for your help, I'm very new to this.
我知道这是错误的,但想了解我正在尝试做什么。任何人都可以提供建议吗?感谢您的帮助,我对此很陌生。
回答by ThinkerIV
What you have would probably do the job. However, it would be extremely inefficient. A better way would be to use a find and replace like this.
你所拥有的可能会完成这项工作。然而,这将是极其低效的。更好的方法是使用这样的查找和替换。
'set the next find to find the red
With Application.FindFormat.Font
.ColorIndex = 3
End With
'set the next find to replace with black
With Application.ReplaceFormat.Font
.ColorIndex = 1
End With
'Do the actual replacing
For Each aSheet In ActiveWorkbook.Worksheets
aSheet.Activate
Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
Next aSheet
This does it for all the cells in all the sheets for the whole workbook. You can also do this by going to the normal finding and replace window then pressing the options button.
这适用于整个工作簿的所有工作表中的所有单元格。您也可以通过转到正常的查找和替换窗口然后按选项按钮来执行此操作。
回答by Nicola Cossu
Sub change_color()
For Each sht In ActiveWorkbook.Sheets
Set rng = sht.UsedRange
For Each cell In rng
If cell.Font.ColorIndex = 3 Then
cell.Font.ColorIndex = 0
End If
Next cell
Next sht
End Sub