2014-07-16 61 views
0

我需要仅为自动着色文本的代码。具体来说,我有干净的文本,然后是蓝色和粗体的文本。我希望干净文本变成红色,粗体和删除线。这是我正在使用的代码,整个单元格都是干净的,蓝色的粗体文本变成红色,粗体和直线。Excel VBA仅为以前未着色的文本着色

Sub KeepBlueBold() 
    'keeps bluebold cell 
    Dim Cell As Range 
    For Each Cell In Selection 
    KeepBlueAddRed Cell 
    Next Cell 
End Sub 


Sub KeepBlueAddRed(Cell As Range) 
Dim iCh  As Integer 
For iCh = 1 To Len(Cell) 
    With Cell.Characters(iCh, 1) 
If .FOnt.ColorIndex <> 1 Then 
Text = Text & .Text 


End If 
End With 
Next iCh 
Cell.Value = Text 
Cell.Characters.FOnt.Strikethrough = True 
Cell.Characters.FOnt.Bold = True 
Cell.Characters.FOnt.ColorIndex = 3 
End Sub 
+0

您是否在任何给定的单元格中使用了混合格式? –

+0

查找和替换不适合? – pnuts

回答

0

下面的代码应该工作,因为你已经选择了一个范围。

Sub KeepBlueBold() 

    Dim c As Range 

    For Each c In Selection.Cells 
     If c.Font.ColorIndex = 1 Then 
      c.Font.ColorIndex = 3 
      c.Font.Bold = True 
      c.Font.Strikethrough = True 
     End If 
    Next 


End Sub 
+0

您是否可以解释“因为您已经选择了一个范围”您的意思是 – user2539359

+0

设置c =范围(“a:AA”) – user2539359

+0

我的意思是说,因为您没有解释您将使用哪个范围,你应该手动选择范围(使用鼠标),然后运行宏。 但是,如果您尝试更改的所有单元都在列A中,则可以输入: Set c = Range(“A:A”) – Bira