简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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