复制行特定条件格式设置公式

时间:2014-07-17 13:34:03

标签: excel vba excel-vba excel-2007 conditional-formatting

我在Excel中有很多数据(2007年,如果这有所不同),我需要有条件地格式化它,以便我W5W115的整体价值会改变颜色等于X5AE567等的总和,因此每行在格式框中需要不同的公式。< / p>

我附上了我正在做的事情的照片。我尝试过在网上执行大量不同的教程,例如删除单元格编号周围的$符号,并使用不同的格式规则,但无法弄明白。

任何人都可以提供帮助吗?

http://i61.tinypic.com/2wgrreo.png

1 个答案:

答案 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

总结一下,我们:

  • 预先定义变量,设置WorksheetLastRow
  • 使用For...Next遍历所有行
    • 将W列中的值指定为MatchAgainst
    • 将列X到AE中的值总和分配给RowSum
    • MatchAgainstRowSum进行比较,突出显示不相等的单元格
相关问题