2011-04-28 28 views
4

我試圖將所有單元格中的所有單元格中的所有紅色文本都更改爲黑色。我正在使用以下內容:循環遍歷工作簿中所有工作表中的所有單元格,如果爲紅色,則更改格式

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 

我知道這是錯誤的,但是想要說明我正在嘗試做什麼。任何人都可以提供意見感謝您的幫助,我對此很新。

回答

0
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 
4

你有什麼可能會做這項工作。但是,這將是非常低效的。更好的方法是使用這樣的查找和替換。

'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 

這會爲在整個工作簿中所有表的所有單元格。您也可以通過轉到正常查找並替換窗口然後按下選項按鈕來完成此操作。

+0

+1。好的解決方案我不會經常玩excel和vba。你能告訴我如何將它應用於所有表單嗎?我嘗試過,但它只適用於活動工作表。 – 2011-04-28 14:56:13

+0

在我的測試中,它適用於所有牀單。我有點困惑,爲什麼它不會在任何情況下。我會檢查一下,看看我能否弄清楚。 – ThinkerIV 2011-04-28 15:13:24

+0

感謝您的有趣。 :)我使用的是excel 2002.也許這種行爲在不同的版本之間有所不同。 – 2011-04-28 15:15:35

相關問題