Excel条件格式化VBA循环

时间:2017-05-07 05:57:36

标签: excel vba excel-vba

我试图通过使用VBA突出显示具有与单元格匹配的值的行来节省一些时间。我可以让它在单行上工作,但希望它能在循环上工作以捕获~97行。

要匹配的值位于U7列:U97中的单元格A4中,因此高亮显示范围(“E7:K7,M7:S7,U7:V7”)

我的代码是:

<div class="container">
  <svg id="blue" width="0" height="0"></svg>
  <svg id="red" width="100" height="100"></svg>
</div>
<div class="container">
  <svg id="red" width="100" height="100"></svg>
</div>

这对于第7行正常工作,我可以剪切并粘贴它并手动更改下一行的值,但必须有一种方法可以在循环中运行它。

我的尝试低于无效,

Sub Macro1()

    Range("E7:K7,M7:S7,U7:V7").Select
    Range("U7").Activate
    Application.CutCopyMode = False
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$U$7=$A$4"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0.249946592608417
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

1 个答案:

答案 0 :(得分:1)

懒惰快速修复(未经测试):

Sub Macro1()

    Dim i As Long

    For i = 7 to 97


    Range(Replace("E#:K#,M#:S#,U#:V#","#",i)).Select
    Range("U" & i).Activate
    Application.CutCopyMode = False
    Selection.FormatConditions.Add Type:=xlExpression, _
                                   Formula1:="=$U$" & i & "=$A$4"      

  Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0.249946592608417
    End With
    Selection.FormatConditions(1).StopIfTrue = False

    Next i

End Sub