【问题标题】:Excel Conditional Formatting VBA LoopExcel条件格式VBA循环
【发布时间】:2017-10-05 07:31:57
【问题描述】:

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

要匹配的值位于 U7:U97 列的单元格 A4 中,因此突出显示 Range("E7:K7,M7:S7,U7:V7")

我的代码是:

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

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

我的尝试在下面不起作用,

Sub Macro2()

    For i = 7 To 97

    Sheet2.Cells(i, "E").Select
    Sheet2.Cells(i, "F").Select
    Sheet2.Cells(i, "G").Select
    Sheet2.Cells(i, "H").Select
    Sheet2.Cells(i, "I").Select
    Sheet2.Cells(i, "J").Select
    Sheet2.Cells(i, "K").Select
    Sheet2.Cells(i, "L").Select
    Sheet2.Cells(i, "U").Activate
    Application.CutCopyMode = False
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=Sheet2.Cells(i, 21)=$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

【问题讨论】:

  • 你为什么使用 VBA,而不是没有它的条件格式?另外,什么不使用代码?是否抛出错误?什么错误,在哪里?此外,您可以通过[避免使用.Select/.Activate](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros). (For instance, in Macro2, the lines selecting columns E through K don't do anything, as you select column L at last, and that becomes Selection`来帮助收紧代码。

标签: vba excel


【解决方案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

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 2011-10-02
    • 2013-04-11
    • 1970-01-01
    • 2016-10-02
    • 2015-10-04
    • 1970-01-01
    • 2017-11-03
    相关资源
    最近更新 更多