2016-06-20 42 views
0

我正在製作發送給代理機構的表單,對於此特定表單,所有總數都假設爲彼此相等。我使用If語句來檢查總數是否相等,但是當使用或連接變量時,在IF語句中,總計中的一個錯誤使得所有字段都變成紅色,因此用戶無法快速找到他們的錯誤。使用和連接變量也會導致一個錯誤,因爲如果在2個單獨的總計中有錯誤,它將不會顯示錯誤。當按下一個按鈕時,我希望程序比較所有總數,並只將與其他紅色不匹配的那些紅色進行比較,反正我有這樣做嗎?其更多的邏輯問題,但這裏是我的代碼:VBA想要轉變不匹配紅色的單元格

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 

順便說一句,用戶按下按鈕,使宏運行。

謝謝

+0

什麼定義了不同的總數?當一半是一個,另一個是另一個時會發生什麼?哪些會突出顯示;所有,更低或更高? –

+0

@ScottCraner這就是用戶可以根據業務需要輸入他們想要的任何數字的問題,但是您提出了一個很好的觀點,在這種情況下突出顯示較低的數字是有意義的,因爲這意味着它們會丟失一些數據如果這些總數少於其他數字的話。 – Artak

回答

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

這將突出那些從多數不同。如果他們都不同,所有都會突出顯示。

+0

非常感謝!我搞砸了你發送了一些東西,以適應我需要的東西,但我無法像你說的那樣使它工作。出現錯誤時,單元格變爲紅色,但不會突出顯示與大多數不同的元素,而是突出顯示所有元素。在我改變你發送的公式之前,對我來說也是如此。有什麼建議? – Artak

+0

如果他們都不同,你不希望所有人都被突出顯示?如果沒有共識,多數人,你怎麼知道該忽略? –

+0

如果它們完全不同,我會希望它突出顯示所有這些,但問題是我除了一個之外都使它們全部相等,但是它導致所有單元變成紅色而不是僅僅是錯誤的。我希望用戶能夠快速查看哪個總數錯誤,所以我只希望總顯示錯誤,就像您在圖片中顯示的那樣。 這就是當我總有一個錯誤時會發生什麼:http://i.stack.imgur.com/743f8.png – Artak