【问题标题】:Sum / subtotal visible cells based on cell or font color基于单元格或字体颜色的总和/小计可见单元格
【发布时间】:2020-10-24 09:18:19
【问题描述】:

我有一张附有许多切片器的桌子。该表格具有条件格式,可根据特定条件将数字列的字体更改为红色或绿色。

我见过许多基于背景颜色计算单元格的函数,但我想做的是;

  • 仅小计范围内与单元格“R2”字体颜色相同的可见单元格
  • 将在单元格“R2”中返回总和
  • 范围是“M9:M200”
  • 然后我需要通过切片器更改运行此操作,因为更新切片器会更改所有数字。

我在这里找到了一些我试图适应但失败的代码。任何帮助表示赞赏

我一直在尝试使用以下代码将条件格式(红色单元格)转换为普通红色,但这也不起作用。

Sub GetColorSum2()
Dim FCELL As Range
    For Each rCell In Range("Y1:Y7").Cells
            If rCell.DisplayFormat.Interior.ColorIndex = 24 Then
                rCell.Font.ColorIndex = 3
            End If
    Next
End Sub

主要代码

Function GetColorsum(sumRange As Range, SumColor As Range, Optional VolatileParameter As Variant)
 Dim ColVal As Long, rCell As Range
 Dim Totalsum As Long
 ColVal = SumColor.Font.ColorIndex
    For Each rCell In sumRange.Cells
      If rCell.Font.ColorIndex = ColVal Then
          If rCell.EntireRow.Hidden = False Then
              If rCell.EntireColumn.Hidden = False Then
              Totalsum = Totalsum + 1
              End If
          End If
      End If
    Next rCell
 GetColorsum = Totalsum
End Function

我也试过

Function Sumclr(rColor As Range, rRange As Range)
    Dim rCell As Range
    Dim lCol As Long
    Dim vResult
    lCol = rColor.Interior.ColorIndex
    For Each rCell In rRange
        If rCell.Interior.ColorIndex = lCol Then
            vResult = WorksheetFunction.Sum(rCell, vResult)
        End If
    Next rCell
    Sumclr = vResult
End Function

【问题讨论】:

  • 使用 CF 产生的颜色只能通过 DisplayFormat 属性(例如 myCell.DisplayFormat.Interior.ColorIndex)获得,但如果您在这里尝试使用 UDF,那么不幸的是,DisplayFormat 在UDF。

标签: excel vba


【解决方案1】:

好的,所以我设法找到了解决方案。基本上我做了三件事,

  1. 将功能替换为按钮。

  2. 将条件格式替换为 VBA 格式。

  3. 使用 IF hidden = false 代码对数字求和

按钮

Sub CreateButton0()
  Dim btn As Button
  Application.ScreenUpdating = False

  Dim t As Range
  Set t = ActiveSheet.Range(Cells(1, 18), Cells(1, 18))
  Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
  
    With btn
      .OnAction = "SUMCOLOUR0"
      .Caption = "POPULATE"
      .Name = "Btn"
      .Font.Size = 8
    End With

  Application.ScreenUpdating = True
End Sub

格式化

Sub HIGHLIGHT()
Dim FCELL As Range
        For Each FCELL In Range("M9:M1000").Cells
            If FCELL.Offset(0, 6) = "YES" Then
                FCELL.Font.ColorIndex = 3
                FCELL.Font.Bold = True
            End If
            If FCELL.Offset(0, -4) = "SR" Then
                FCELL.Font.ColorIndex = 10
                FCELL.Font.Bold = True
            End If
    Next FCELL
End Sub

求和

Sub SUMCOLOUR0()
 Dim RCELL As Range
 Dim SCELL As Range
 Dim RRESULT As Double
 Dim SRESULT As Double
    ''''SUM OTE (RED)''''
    For Each RCELL In Range("M9:M1000").Cells
        If RCELL.Font.ColorIndex = 3 Then
          If RCELL.EntireRow.Hidden = False And RCELL.EntireColumn.Hidden = False Then
                RRESULT = WorksheetFunction.SUM(RCELL, RRESULT)
          End If
      End If
    Next RCELL
    Cells(2, ActiveSheet.Shapes("BTN").TopLeftCell.Column).Value = RRESULT
    
    ''''SUM SW (GREEN)''''
    For Each SCELL In Range("M9:M1000").Cells
        If SCELL.Font.ColorIndex = 10 Then
          If SCELL.EntireRow.Hidden = False And SCELL.EntireColumn.Hidden = False Then
                SRESULT = WorksheetFunction.SUM(SCELL, SRESULT)
          End If
      End If
    Next SCELL
    Cells(3, ActiveSheet.Shapes("BTN").TopLeftCell.Column).Value = SRESULT
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2018-02-13
    • 1970-01-01
    • 2015-11-29
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多