简体   繁体   English

查找值并计数表中的出现次数

[英]Finding values and counting occurences in a table

I'm trying to create a function or a macro that lists the different values in a table and how many occurrences of the value is in the table. 我正在尝试创建一个函数或宏,该函数或宏列出表中的不同值以及该表中出现了多少次该值。

For example if i have a table like this logging daily stops for machinery. 例如,如果我有一个这样的表,则每天记录机械停机时间。

          |   Day 1    |   Day 2     |    Day 3     |
          |:----------:|:-----------:|:------------:|
Machine 1 |     a      |     a       |   No stop    |
Machine 2 |     d      |     g       |      b       |
Machine 3 |     e      |     c       |   No stop    |
Machine 4 |     g      |     i       |      a       |
Machine 5 |     c      |     a       |      b       |
Machine 6 |     a      |     b       |      c       |

(The letters ai represent different types of stops) (字母ai代表不同类型的止损)

I want to create a list like this. 我想创建一个这样的列表。

|   Stop     | Occurances |
|:----------:|:----------:|
|     a      |      5     |
|  No stop   |      2     |
|     d      |      1     |
|     g      |      2     |
|     b      |      3     |
|     e      |      1     |
|     c      |      3     |
|     i      |      1     |

I have already found a ways to do this on a single single column range i'm having trouble doing this on a multi column range. 我已经找到了在单个单列范围内执行此操作的方法,但是在多列范围内执行此操作时遇到了麻烦。

You could modify the code and try: 您可以修改代码并尝试:

Option Explicit

Sub test()

    Dim rng1 As Range, rng2 As Range, cell As Range
    Dim Lastrow As Long, Times1 As Long, Times2 As Long

    'Indicate which sheet you will use
    With ThisWorkbook.Worksheets("Sheet1")
        'Set the range where all data appears
        Set rng1 = .Range("B2:D7")
        'Loop the range
        For Each cell In rng1
            'Find the last row of the column with the results
            Lastrow = .Cells(.Rows.Count, "F").End(xlUp).Row
            'Set the range will all the stops
            Set rng2 = .Range("F2:F" & Lastrow)
            'Count how many times a stop appears in the results
            Times1 = Application.WorksheetFunction.CountIf(rng2, cell.Value)
            'If it does appears regord
            If Times1 = 0 Then

                Times2 = Application.WorksheetFunction.CountIf(rng1, cell.Value)
                .Range("F" & Lastrow + 1).Value = cell.Value
                .Range("G" & Lastrow + 1).Value = Times2

            End If

        Next cell

    End With

End Sub

While the solution that i found is not a macro/formula i found that the simplest and quickest way to solve this was to unpivot the table. 虽然我找到的解决方案不是宏/公式,但我发现解决此问题的最简单,最快的方法是取消表的摆放。 I did this by selecting the entire table, Data->From Table/Range and in the Power Query Editor Transform->Unpivot Columns. 我通过选择整个表Data-> From Table / Range并在Power Query Editor Transform-> Unpivot Columns中进行了此操作。

This gave me all the stops in a single column which was much more manageable. 这使我在单一列中的所有停靠点更加易于管理。

Using Data->Remove Duplicates i could get the unique stops and could use a simple 使用数据->删除重复项,我可以获得唯一的停靠点,并且可以使用简单的

=COUNTIF(RANGE_TABLE;STOP_NR)

to get occurrences of stops. 得到停止的发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM