【问题标题】:Count Color Changes In Excel Row计算 Excel 行中的颜色变化
【发布时间】:2018-07-26 13:23:05
【问题描述】:

我有一个记录项目时间的电子表格。每个项目都分配了一种颜色。一行包含半小时增量。每半小时都会根据当时正在进行的项目分配一种颜色。

我想计算行长从一种颜色到另一种颜色的变化次数。一排就是一天。

如何在 Excel 中执行此操作?

下面是一行的样子。每一行是一天。我需要计算从左到右这行颜色改变了多少次。所以这种情况下的输出将是 7,因为项目更改了 7 次。

【问题讨论】:

  • 能否请您举例说明输入数据和您希望获得的输出?另外,颜色是否使用条件格式规则完成?
  • 添加了一个带有预期输出的示例行输入。
  • 我建议添加 vba 标签,因为没有其他方法可以实现这一点。

标签: vba excel colors count


【解决方案1】:

这是一个稍微精简的 UDF:

Function CountColorChanges(rng As Range) As Integer
    Dim i As Integer

    With rng.Columns
        If .Count > 1 Then
            For i = 1 To .Count - 1
                If rng.Cells(1, i + 1).Interior.Color <> rng.Cells(1, i).Interior.Color Then CountColorChanges = CountColorChanges + 1
            Next i
        End If
    End With
End Function

【讨论】:

    【解决方案2】:

    Option Explicit
    
    Function NumberOfColorChanges(ByVal rng As range)as long
        Dim cell As range
        Dim color As Long
        Dim firstCell As Boolean
        firstCell = True
        For Each cell In rng
            If firstCell = True Then
                color = cell.Interior.color
                firstCell = False
            Else
                If color <> cell.Interior.color Then
                    NumberOfColorChanges = NumberOfColorChanges + 1
                    color = cell.Interior.color
                End If
            End If
        Next cell
    End Function
    

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 2010-09-24
      • 1970-01-01
      • 2014-08-21
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多