【问题标题】:Excel VBA Conditional Formatting formula within formula公式中的Excel VBA条件格式公式
【发布时间】:2014-12-27 23:34:44
【问题描述】:

我被卡住了,我不知道是否有任何解决方案,因为我尝试了很多并且一直失败。

我正在尝试添加条件格式,因此如果 B 列中的单元格等于或小于 A 列中合作伙伴单元格的 19%,则单元格变为黄色。因此在上面的示例中,单元格 B1 应该变为黄色,因为 $19,000 更少大于或等于 100,000 美元的 19%。

我需要通过 excel vba 添加这个条件格式。我尝试在下面添加 vba 代码,但 B1:B3 中所有单元格的条件格式公式都被 $A1*0.19 卡住了。我需要 B1 条件格式公式为 $A1*0.19,然后 B2 条件格式公式为 $A2*0.19 等等等等。顺便说一句,我有大约 350 行,而不仅仅是 3 行。即便如此,我的 vba 代码还是变成了 $A521325*0.19 或与真实或实际不同的东西。

With Sheet1.Range(Sheet1.Cells(1, 2), Sheet1.Cells(3, 2))
    daformula = "=$A1*0.19"
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, Formula1:=daformula
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
        .FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
        .FormatConditions(1).Interior.ThemeColor = xlThemeColorAccent2
        .FormatConditions(1).Interior.TintAndShade = -0.249946592608417
    .FormatConditions(1).StopIfTrue = False
End With 

这个想法是在运行将条件格式添加到工作表的宏之后,当用户更改 B 列中的一个单元格时,颜色会消失或重新出现,具体取决于用户将单元格更改为的值(条件格式必须仍然工作)

【问题讨论】:

  • 我已经测试了你上面的代码,效果很好。工作正常HERE
  • 不,恐怕不是。使用链接中的该文件,尝试删除所有条件格式,然后再次运行代码。您会看到 38 个中有 200 个没有变红,其他的也没有变红。搞砸了这似乎确实很难:s
  • 不知道该告诉你什么,代码对于 Excel 2010 是正确的。如果你有不同的版本,我想我没有多大帮助:(
  • 是的,我有 Excel 2007:s

标签: vba excel


【解决方案1】:

试试,

With ActiveSheet.Cells(1, 2).Resize(3, 1)
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$B1<=($A1*0.19)"
    .FormatConditions(.FormatConditions.Count).Interior.Color = 65535
End With

是否添加.SetFirstPriority.StopIfTrue 等其他详细信息在一定程度上取决于其他CF 规则如何影响相同的单元格。

【讨论】:

    【解决方案2】:

    您可以创建一个事件来监控 A 列中的任何更改(将此代码放在 Sheet1 对象中)

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Column = 1 Then
    
            'run code to add conditional formatting
            condFormat Target
    
        End If
    
    End Sub
    

    现在我们只需要更改上面的代码以接受目标,并且只为 B 列中的等效单元格添加格式

    Public sub condFormat (Target as range)
        With target.offset(0,1)
            daformula = "=" & target.address & "*0.19"
            .FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
                                  Formula1:=daformula
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            .FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
            .FormatConditions(1).Interior.ThemeColor = xlThemeColorAccent2
            .FormatConditions(1).Interior.TintAndShade = -0.249946592608417
            .FormatConditions(1).StopIfTrue = False
        End With
    end sub
    

    我没有考虑一次更改超过 1 个单元格,但这将解决您的挑战

    【讨论】:

    • 我忘了说,整个数据库正在 Excel VBA 中处理,然后上传到 Google Docs。这就是为什么我们需要 CF,因为上传到 Google Docs 时也会包含 CF
    【解决方案3】:

    从 VBA 创建条件格式时,与条件格式公式相关的单元格选择至关重要。

    如果条件格式公式仅访问同一行上的值,请尝试使用 ActiveSheet.cells(1,1).select,如果它引用上面行的值,请尝试使用 ActiveSheet.cells(2,1)。

    【讨论】:

      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      相关资源
      最近更新 更多