简体   繁体   English

循环遍历工作簿中所有工作表中的所有单元格,如果是红色则更改格式

[英]loop through all cells in all sheets in workbook, change format if red

I'm trying to change all red text to black in all cells for all sheets of an excel workbook.我正在尝试将 excel 工作簿的所有工作表的所有单元格中的所有红色文本更改为黑色。 I'm using the following:我正在使用以下内容:

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.感谢您的帮助,我对此很陌生。

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.您也可以通过正常查找并替换 window 然后按选项按钮来执行此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM