计算Excel行中的颜色变化

时间:2018-07-26 13:23:06

标签: excel vba excel-vba colors count

我有一个电子表格,可以跟踪项目时间。每个项目都分配有一种颜色。一行包含半小时的增量。每个半小时都会根据该时间段内正在处理的项目分配一种颜色。

我想计算从一种颜色到另一种颜色在行的长度上的变化次数。连续一天。

如何在Excel中执行此操作?

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

enter image description here

2 个答案:

答案 0 :(得分:3)

这是一个更简洁的UDF:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="txtorigines"></textarea>
<textarea id="txtclean"></textarea>
<button type="button" id="launchCleaner">Nettoyer le texte</button>

答案 1 :(得分:1)

enter image description here

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