首页 > 解决方案 > 每次刷新页面时循环更改特定单元格中字体颜色的范围

问题描述

我已经编写了这段代码作为开始。我认为我走在正确的道路上,但代码显然需要更多才能工作。基本上我想从单元格 W8 开始:

    SubFontW8()
    If Range("W8").Font.Color = vbRed Then
        Range("W8").Font.Color = xlAutomatic
    And
    Range("W9").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontW9()
    End If
    End Sub
    
    SubFontW9()
    If Range("W9").Font.Color = vbRed Then
        Range("W9").Font.Color = xlAutomatic
    And
    Range("W10").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontW10()
    End If
    End Sub
    
    SubFontW10()
    If Range("W10").Font.Color = vbRed Then
        Range("W10").Font.Color = xlAutomatic
    And
    Range("W11").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontW11()
    End If
    End Sub
    
    SubFontW11()
    If Range("W11").Font.Color = vbRed Then
        Range("W11").Font.Color = xlAutomatic
    And
    Range("W12").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontW12()
    End If
    End Sub
    
    SubFontW12()
    If Range("W12").Font.Color = vbRed Then
        Range("W12").Font.Color = xlAutomatic
    And
    Range("Y8").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontY8()
    End If
    End Sub
    
    SubFontY8()
    If Range("Y8").Font.Color = vbRed Then
        Range("Y8").Font.Color = xlAutomatic
    And
    Range("Y9").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontY9()
    End If
    End Sub
    
    SubFontY9()
    If Range("Y9").Font.Color = vbRed Then
        Range("Y9").Font.Color = xlAutomatic
    And
    Range("Y10").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontY10()
    End If
    End Sub
    
    SubFontY10()
    If Range("Y10").Font.Color = vbRed Then
        Range("Y10").Font.Color = xlAutomatic
    And
    Range("Y11").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontY11()
    End If
    End Sub
    
    SubFontY11()
    If Range("Y11").Font.Color = vbRed Then
        Range("Y11").Font.Color = xlAutomatic
    And
    Range("Y12").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontY12()
    End If
    End Sub
    
    SubFontY12()
    If Range("Y12").Font.Color = vbRed Then
        Range("Y12").Font.Color = xlAutomatic
    And
    Range("W8").Font.Color = vbRed
    Exit Sub
    Else
    Call SubFontW8()
    End If
    End Sub

您的任何帮助将不胜感激。谢谢你。

标签: excelvba

解决方案


有几种方法可以完成此任务。首先,您将需要一个For Next Loop。在此上下文中,您可以使用 aFor Next或 a For Each。我建议阅读更多关于循环的内容以更好地理解。

为简单起见,我将使用For Each循环。

请参见下面的代码(插入到 VBE 中的任何普通模块中):

Option Explicit

Sub Change_Font_Colour()
    
    'range where you want to change the font colours
    Dim rOperationRange As Range
    'variable used to loop through the operation range
    Dim cell As Range
    
    
    'set range object
    'you should ALWAYS have a worksheet qualifier
    'change the sheet name to the required sheet name and adjust your range accordingly
    Set rOperationRange = ThisWorkbook.Sheets("mySheetName").Range("W8:Y12")
    
    
    'for loop
    For Each cell In rOperationRange
        If cell.Font.Color = vbRed Then
            'set to black
            cell.Font.Color = vbBlack
            'set cell BELOW as red using .Offset
            cell.Offset(1, 0).Font.Color = vbRed

            'now exit
            Exit Sub
        End If
    Next cell
    
    
End Sub

推荐阅读