簡體   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