【问题标题】:VBA Want to turn cells that don't match redVBA想要将不匹配的单元格变为红色
【发布时间】:2016-06-20 16:01:08
【问题描述】:

我正在制作一个发送给代理机构的表格,对于这张特定的表格,所有总数都应该是相等的。我使用 If 语句检查总数是否都相等,但是在使用 Or 连接变量时,在 IF 语句中,总数中的一个错误会使所有字段都变红,因此用户无法快速找到错误。使用 And 连接变量也会导致错误,因为如果 2 个单独的总计中有错误,则根本不会显示错误。当按下一个按钮时,我希望程序比较所有总数并只将那些与其他总数不匹配的那些设为红色,无论如何我可以这样做吗?它更像是一个逻辑问题,但这是我的代码:

Sheets("Initiated Proceedings").Select
Sheet3.Unprotect


DisputantTotal = Range("I19").Value
DisputantFrequency = Range("I24").Value
GenderTotal = Range("I31").Value
AgeTotal = Range("I39").Value
EthnicityTotal = Range("I50").Value
LanguageTotal = Range("I65").Value
IncomeTotal = Range("I73").Value
DisabilityTotal = Range("I79").Value
HomelessTotal = Range("I85").Value


If DisputantTotal <> DisputantFrequency Or DisputantTotal <> GenderTotal Or DisputantTotal <> AgeTotal Or DisputantTotal <> EthnicityTotal Or DisputantTotal <> LanguageTotal Or DisputantTotal <> IncomeTotal Or DisputantTotal <> HomelessTotal Or DisputantTotal = 0 Then



Range("I19").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

Else

Range("I19").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With

End If


If DisputantTotal <> DisputantFrequency Or DisputantFrequency <> GenderTotal Or DisputantFrequency <> AgeTotal Or DisputantFrequency <> EthnicityTotal Or DisputantFrequency <> LanguageTotal Or DisputantFrequency <> IncomeTotal Or DisputantFrequency <> HomelessTotal Or DisputantFrequency = 0 Then



Range("I24").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

Else

Range("I24").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With

End If



If DisputantTotal <> GenderTotal Or DisputantFrequency <> GenderTotal Or GenderTotal <> AgeTotal Or GenderTotal <> EthnicityTotal Or GenderTotal <> LanguageTotal Or GenderTotal <> IncomeTotal Or GenderTotal <> HomelessTotal Or GenderTotal = 0 Then



Range("I31").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

Else

Range("I31").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With

End If



If DisputantTotal <> AgeTotal Or DisputantFrequency <> AgeTotal Or GenderTotal <> EthnicityTotal Or AgeTotal <> LanguageTotal Or AgeTotal <> IncomeTotal Or AgeTotal <> HomelessTotal Or AgeTotal = 0 Then



Range("I39").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

Else

Range("I39").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With

End If


If DisputantTotal <> EthnicityTotal Or DisputantFrequency <> EthnicityTotal Or EthnicityTotal <> AgeTotal Or EthnicityTotal <> LanguageTotal Or EthnicityTotal <> IncomeTotal Or EthnicityTotal <> HomelessTotal Or EthnicityTotal = 0 Then



Range("I50").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

Else

Range("I50").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With

End If


If DisputantTotal <> LanguageTotal Or DisputantFrequency <> LanguageTotal Or LanguageTotal <> AgeTotal Or LanguageTotal <> EthnicityTotal Or LanguageTotal <> IncomeTotal Or LanguageTotal <> HomelessTotal Or LanguageTotal = 0 Then



Range("I65").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

Else

Range("I65").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With

End If



If DisputantTotal <> IncomeTotal Or DisputantFrequency <> IncomeTotal Or IncomeTotal <> AgeTotal Or IncomeTotal <> EthnicityTotal Or IncomeTotal <> HomelessTotal Or IncomeTotal = 0 Then



Range("I73").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

Else

Range("I73").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With

End If


If DisputantTotal <> HomelessTotal Or DisputantFrequency <> HomelessTotal Or HomelessTotal <> AgeTotal Or HomelessTotal <> EthnicityTotal Or IncomeTotal <> HomelessTotal Or HomelessTotal = 0 Then



Range("I85").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

Else

Range("I85").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With

End If








ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, AllowInsertingHyperlinks:=True, AllowSorting:= _
        True, AllowFiltering:=True, AllowUsingPivotTables:=True


Range("I85").Select


End Sub

顺便说一句,用户按下按钮使宏运行。

谢谢

【问题讨论】:

  • 什么定义了不同的总数?当一半是一个而其他是另一个时会发生什么?哪个会突出显示;全部,低还是高?
  • @ScottCraner 这就是用户可以根据业务输入任何他们想要的数字的问题,但是你提出了一个很好的观点,在这种情况下突出显示较低的数字是有意义的,因为它会意味着如果这些总数小于其他数据,他们会在表单中丢失一些数据。

标签: vba validation loops if-statement


【解决方案1】:

您可以使用条件格式来执行此操作。使用这个公式:

=SUMPRODUCT(($I$19=I19)+($I$24=I19)+($I$31=I19)+($I$39=I19)+($I$50=I19)+($I$65=I19)+($I$73=I19)+($I$79=I19)+($I$85=I19))<5

确保它仅适用于总数:

=$I$19,$I$24,$I$31,$I$39,$I$50,$I$65,$I$73,$I$79,$I$85

它将突出显示与大多数人不同的那些。如果它们都不同,所有都将突出显示。

【讨论】:

  • 非常感谢!我弄乱了你发送的一些东西以适应我需要的东西,但我无法让它完全像你说的那样工作。出现错误时,单元格会变为红色,但它不会仅突出显示与大多数不同的单元格,而是突出显示所有单元格。在我更改您发送的公式的任何内容之前,这对我来说也是如此。有什么建议吗?
  • 如果它们都不同,您不希望所有这些都被突出显示吗?如果没有达成共识,多数,你怎么知道忽略哪个?
  • 如果它们都不同,我希望它突出显示所有这些,但问题是我让它们都相等,除了一个,但它导致所有单元格变成红色,而不仅仅是一个那是错的。我希望用户能够快速看到哪个总数是错误的,所以我只希望像您在图片中那样突出显示错误的总数。当我有一个错误的总数时会发生这种情况:i.stack.imgur.com/743f8.png
  • 其他人呢?您的代码中有 9 个单元格,您要测试多少个?
  • 试试这个公式,它会标记所有不等于最大数的; =I19&lt;&gt;MAX($I$19,$I$24,$I$31,$I$39,$I$50,$I$65,$I$73,$I$79,$I$85)
猜你喜欢
  • 2018-06-02
  • 2023-04-02
  • 2013-06-04
  • 2015-06-18
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
相关资源
最近更新 更多