计算出黄色细胞的出现;在读取紫色时,代码输出计数和循环

时间:2017-05-22 21:38:38

标签: excel vba excel-vba

我正在尝试创建一个循环查看如下所示数据的函数:

The purple represents the ycolor in the code; when the code reads this color I want the count to be displayed and then reset to zero. This is because the purple represents a new data set (note: there are 7,541 purple lines in the spreadsheet, hence why I want to use a looping VBA function rather than counting). The yellow represents the xcolor in the code; when the code reads this color I want the count to begin and add +1 for every yellow cell found between purple cells. With the code running correctly I would want it to display a 6 for the first purple row, and a 2 for the second purple row.

总的来说,电子表格中有185,000行,因此显示器不需要位于特定位置,它可能只是一个数字数组,我可以绘制成直方图以显示范围频率。

Function CountCcolor(range_data As Range, criteria As Range, log_page As Range) As Long
    Dim datax As Range
    Dim xcolor As Long
    Dim ycolor As Long

xcolor = criteria.Interior.ColorIndex
ycolor = log_page.Interior.ColorIndex

For Each datax In range_data
    If datax.Interior.ColorIndex = xcolor Then
        CountCcolor = CountCcolor + 1
        ElseIf datax.Interior.ColorIndex = ycolor Then
            Debug.Print CountCcolor
            CountCcolor = 0
    End If
Next datax
End Function

在代码中,range_data是注释列,通过选择仅包含黄色的空单元格来选择条件,并且还通过选择仅包含紫色的空单元格来选择log_page(log_page表示我在哪里希望代码打印总数,清除计数,然后循环)。

我已经能够编写一个能够正确计算整个电子表格中所有黄色行的函数;但是,根据紫色的行,无法使其循环并正确显示输出。

1 个答案:

答案 0 :(得分:0)

作为从另一个程序调用的子程序:

super()

输入和输出:

enter image description here