【问题标题】:Copying row specific conditional formatting formula [closed]复制行特定的条件格式公式[关闭]
【发布时间】:2014-09-08 09:57:04
【问题描述】:

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

我附上了一张我正在做的事情的图片。我尝试了很多不同的在线教程,比如删除单元格编号周围的$ 符号并使用不同的格式规则,但就是想不通。

任何人都可以帮忙吗?

【问题讨论】:

  • 您的报告格式是否已锁定,或者您是否可以添加一个辅助列(例如 AF),将 XAE 相加?
  • 不幸的是,这似乎不是关于编程的问题,而是关于 Excel 的一般使用问题,因此在本网站上是题外话。您可以通过 SuperUser 寻求帮助。
  • @pnuts 这个问题不是关于 Excel 公式的,而是关于条件格式的。提问者也没有实际说明或证明他已经尝试或正在寻找 VBA 解决方案,这让我相信这个问题被错误地标记了。在我发表原始评论时,这个问题没有答案。 [conditional-formatting] 标签描述甚至指出,如果不是专门关于从 VBA/VSTO/Similar 访问条件格式,您应该考虑向 SuperUser 提问。

标签: vba excel excel-2007 conditional-formatting


【解决方案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,如果不相等则突出显示单元格

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-31
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多