繁体   English   中英

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

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

我正在尝试将 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

我知道这是错误的,但想说明我正在尝试做什么。 任何人都可以提供建议吗? 感谢您的帮助,我对此很陌生。

你所拥有的可能会完成这项工作。 然而,这将是极其低效的。 更好的方法是像这样使用查找和替换。

'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

这适用于整个工作簿的所有工作表中的所有单元格。 您也可以通过正常查找并替换 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