2011-04-28 49 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

相关问题