VBA想要转换不匹配红色的单元格

时间:2016-06-20 16:01:09

标签: vba validation loops if-statement

我正在制作一份发送给代理机构的表格,对于这张特定的表格,所有总数都假设彼此相等。我使用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

BTW用户按下按钮使宏运行。

由于

1 个答案:

答案 0 :(得分:0)

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

=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

enter image description here

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