我在Excel中有很多数据(2007年,如果这有所不同),我需要有条件地格式化它,以便我W5
到W115
的整体价值会改变颜色等于X5
到AE5
,6
,7
等的总和,因此每行在格式框中需要不同的公式。< / p>
我附上了我正在做的事情的照片。我尝试过在网上执行大量不同的教程,例如删除单元格编号周围的$
符号,并使用不同的格式规则,但无法弄明白。
任何人都可以提供帮助吗?
答案 0 :(得分:1)
如果您想在不使用帮助列的情况下解决此问题,可以使用如下的小脚本:
Option Explicit
Sub HighlightDiscrepancies()
Dim LastRow As Long, Counter As Long
Dim RowSum As Double, MatchAgainst As Double
Dim MySheet As Worksheet
'set references up-front
Set MySheet = ThisWorkbook.Worksheets("Sheet1") '<~ assume data on Sheet1
With MySheet
LastRow = .Cells.Find("*", _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
End With
'loop through each row, comparing the sum of X through AE to
'the value in W
With MySheet
For Counter = 1 To LastRow
'set the value in col W to the match variable below
MatchAgainst = .Cells(Counter, 23).Value
'set the sum of col X to col AE in the sum variable below
RowSum = Application.Sum(.Range(.Cells(Counter, 24), .Cells(Counter, 31)))
'use an if statement to compare values, making the cell red
'if the sum variable is not equal to the match variable
If MatchAgainst <> RowSum Then
.Cells(Counter, 23).Interior.Color = RGB(255, 0, 0)
End If
Next Counter
End With
End Sub
总结一下,我们:
Worksheet
和LastRow
For...Next
遍历所有行
MatchAgainst
RowSum
MatchAgainst
与RowSum
进行比较,突出显示不相等的单元格