【问题标题】:How to count color patterns in excel with vba如何用vba计算excel中的颜色模式
【发布时间】:2018-02-20 17:08:17
【问题描述】:

我有一个包含 6 列和大约 247 行的电子表格。这些行中的每一行在每列中都有整数,颜色为红色、黄色或蓝色。这表明每个单元格中的数字在它出现的次数内是热的、中的或冷的。我想计算模式。因此,如果出现一行(蓝色、黄色、黄色、黄色、红色、黄色),我想计算一下该模式发生了多少次。它必须是特定于颜色的列。我有一组包含所有可能模式的列和行,并希望通过它对应的模式显示计数。所以也许比较会起作用?我已经很久没有使用VBA了。如果已经有答案,我很抱歉,但我的搜索没有出现任何结果。谢谢你。

2018 年 2 月 19 日更新 11:56 - 添加了图片。 这是数据的顶部。此时颜色不会自动填充到单元格中。但是,当添加新数据时,计数必须更新。

【问题讨论】:

  • 我们需要查看您的数据是如何设置的,尤其是您的可能模式是如何在工作表上设置的。能给我们提供一张照片吗?

标签: excel vba


【解决方案1】:

如果我理解正确,那么以下内容应该符合您的预期:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
ws.Columns(14).ClearContents 
For i = 3 To lastrow 'loop from row 3 to last row with data on Column A
    Color1 = ws.Cells(i, 1).Interior.Color 'get the interior color of the cell
    Color2 = ws.Cells(i, 2).Interior.Color
    Color3 = ws.Cells(i, 3).Interior.Color
    Color4 = ws.Cells(i, 4).Interior.Color
    Color5 = ws.Cells(i, 5).Interior.Color
    Color6 = ws.Cells(i, 6).Interior.Color
    For x = 3 To 732
    'loop from row 3 to 732, amend this to however many color combinations you have on Columns H:M (I assumed 3 Color possibilities in 6 cells, so 3 to the power of 6 = 729)
        If Color1 = ws.Cells(x, 8).Interior.Color And Color2 = ws.Cells(x, 9).Interior.Color And _
        Color3 = ws.Cells(x, 10).Interior.Color And Color4 = ws.Cells(x, 11).Interior.Color And _
        Color5 = ws.Cells(x, 12).Interior.Color And Color6 = ws.Cells(x, 13).Interior.Color Then
        'if all 6 colors match then
            ws.Cells(x, 14) = Val(ws.Cells(x, 14)) + 1
        End If
    Next x
Next i
End Sub

【讨论】:

  • 计数存储在哪里?应该在 Total 列中,表明模式匹配(x 次)。我运行了代码,它似乎没有产生任何东西。也很抱歉,我错过了“模式组合”中的一列 H - M 应该是颜色,然后数据将进入 N 列。
  • 总计数将在您提到的 N 列中继续,...如何使用填充颜色或使用条件格式对单元格进行着色?
  • 我现在可以使用它了。这是因为我没有图表上的最后一行颜色。无论如何在运行时清除计数?目前,如果我再次运行代码,它只会使计数加倍。每次运行它时,我都必须清除单元格。谢谢
  • @ScottPurtan 更新了我的答案以在再次运行代码之前清除 N 列的内容。 :)
【解决方案2】:

如果我可以提供 Xabier 答案的替代方案,我认为可行的方法是为每个行模式提供唯一的参考。

我们可以使用布尔代数为行分配一个数字。下面的代码将添加一列与行中彩色单元格图案相对应的参考编号。

您需要将三个变量coldmedhot 的值更改为单元格的相应颜色值。你可以使用range.interior.color找到这个

 Sub cellcolor()
    Dim x As Long, y As Long
    Dim firstrow As Long, lastrow As Long, lastcol As Long
    Dim ptn As Long
    Dim cold As Long: cold = 9851952  'Replace with your own colour
    Dim med As Long: med = 49407      'Replace with your own colour
    Dim hot As Long: hot = 192        'Replace with your own colour

    'Find the first row
    firstrow = 1
    Do While Cells(firstrow, "A") = ""
        firstrow = firstrow + 1
    Loop

    'Find the last row
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    'Find the last column
    lastcol = Cells(firstrow, "A").End(xlToRight).Column

    For x = firstrow To lastrow
        ptn = 0
        For y = 1 To lastcol
            Select Case Cells(x, y).Interior.Color
                Case cold
                    ptn = ptn + (2 ^ ((3 * y - 2) - 1))
                Case med
                    ptn = ptn + (2 ^ (3 * y - 2))
                Case hot
                    ptn = ptn + (2 ^ ((3 * y - 2) + 1))
            End Select
        Next y
        'Print the result to the column right of the last column
        Cells(x, lastcol + 1).Value = ptn
     Next x
 End Sub

同样的方法可以应用于模式组合列表,为每一行提供一个与其模式相对应的唯一编号(偏移y 的值,例如y+7 并更改lastcol)。然后可以使用=COUNTIF() 将这些数字与原始列表中的结果进行交叉引用,以告诉您它发生了多少次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2018-11-03
    • 2021-12-10
    相关资源
    最近更新 更多