简体   繁体   English

如何计算Excel中数据模式的出现次数?

[英]How do I count occurrences of a pattern of data in Excel?

I realized after manual scanning 90 records, that this was going to be painful and tedious unless I made use of automation. 手动扫描90条记录后,我意识到,除非使用自动化,否则这将是痛苦而又乏味的。

I have this set of data, about 4000 records that occurs in a pattern that I want to track. 我有这组数据,大约有4000条以我要跟踪的模式发生的记录。 The first column is the important one. 第一栏很重要。 I want to scan through the column and record in a NEW column how man times that number has occurred. 我想浏览该列,并在NEW列中记录该数字出现的人工倍数。 Is it possible to do this programatically in Excel? 是否可以在Excel中以编程方式执行此操作?

Note: I'm not just looking for a single pattern or single occurrence of a pattern. 注意:我不仅在寻找单一模式或单一模式的出现。

Eg in this sample 313 occurs 1 time, 314 occurs 6 times, 315 occurs 2 times, etc. 例如,此样本中313发生1次,314发生6次,315发生2次,依此类推。

At the end of the occurrences I want it to look like 在事件结束时,我希望它看起来像

--- Desired Output -------

313 1       343  1
314 1   344  
314 2   344 
314 3   344
314 4   344
314 5   344  
314 1   345  6
315 2   345  
315 1   346  2


-- Sample Data ------------------------------------
313 1   343
314 1   344
314 2   344
314 3   344
314 4   344
314 5   344
314 1   345
315 2   345
315 1   346
316 2   346
316 1   347
317 2   347
318 1   348
318 2   348
319 1   349
319 2   349
319 3   349  

5/23/13 The data is delimited by the spaces. 13/5/23数据由空格分隔。 It is not all in one cell. 并非全部都在一个单元中。 I don't know how to create a grid picture here. 我不知道如何在此处创建网格图片。 The leftmost cell is the one I want counted. 最左边的单元格是我要计算的单元格。

The desired output is the example of what I want. 所需的输出是我想要的示例。 There are six occurrences of 314, I want the count summary cell to be compiled in the row of the last occurrence. 有314次出现的6个事件,我希望将计数摘要单元格编译到最后一个事件的行中。

I backed up, slowed down and went to some basic programming principles, as slow as they feel at times. 我备份,放慢了脚步,并开始学习一些基本的编程原理,这些原理有时会感到有些慢。

  1. Flowchart 流程图
  2. Pseudocode 伪代码
  3. prototype 原型
  4. test 测试
  5. repeat 3 and 4 as needed. 根据需要重复3和4。

I found that the following code did EXACTLY what I needed. 我发现以下代码完全符合我的需要。 I share it for any who follow. 我将其分享给所有关注者。

Sub countFoo()
Dim startCell As Range
Dim preCell As Range
Dim counter As Integer
Dim startPoint As Range, endPoint As Range
Dim fileName As String, delimitingCharacter As String, SQLpre As String, SQLpost As String
Dim SQL As String
Dim outfile As Integer

fileName = "update_foo.sql"
SQLpre = "UPDATE foo SET foodata = "
SQLpost = " WHERE details = '"
outfile = FreeFile()
Open fileName For Output As outfile
counter = 1

Set startPoint = Cells(2, 4)
startPoint.Activate

Debug.Print "Start Point:" & startPoint.Address
Debug.Print startPoint.Value

Set startCell = ActiveCell
Set preCell = startCell.Offset(-1, 0)


Do While startCell.Value <> "END"

If (startCell.Value = preCell.Value) Then
  counter = counter + 1
  Set preCell = startCell
  Set startCell = startCell.Offset(1, 0)
ElseIf ((startCell.Value <> preCell.Value) Or (startCell.Value = "END")) Then
  startCell.Offset(-1, 3).Value = counter
  If counter > 1 Then
    startCell.Offset(-1, 0).Interior.Color = 5296274
    startCell.Offset(-1, 1).Interior.Color = 5296274
    startCell.Offset(-1, 2).Interior.Color = 5296274
    startCell.Offset(-1, 3).Font.Bold = True
    With startCell.Offset(-1, 3).Interior
      .Pattern = xlGray8
      .PatternColor = 65535
      .Color = 5296274
    End With
  End If
  SQL = SQLpre & counter & SQLpost & startCell.Offset(-1, 0).Value & "';"
  Print #outfile, SQL
  counter = 1
  Set preCell = startCell
  Set startCell = startCell.Offset(1, 0)
End If
Loop
Close #outfile
End Sub

If all you want to do is count the number of ocurrences of a certain numer in a certain range all you have to do is use COUNTIF(range,criteria) 如果您只想计算某个数字在一定范围内的出现次数,那么您要做的就是使用COUNTIF(range,criteria)

where range is the cells where you want to check ( according to you it would be "A1:A4000") and criteria is the number you are loonking for, it can also be an ocrrence like ">55" where it counts how many cells the value is bigger than 55. 这里的range是要检查的单元格(根据您的说法,它是“ A1:A4000”),而条件是您要查找的数字,它也可以是“> 55”这样的次数,它可以计算多少个单元格该值大于55。

Hope it helps, Bruno 希望能有所帮助,布鲁诺

The code i mentioned in the comment: 我在评论中提到的代码:

CurrentRowA = 1
LastRowA = Range("A50000").End(xlUp).Row
Dim r As Range
While CurrentRowA <= LastRowA
    CurrentRowB = 1
    LastRowB = Range("B50000").End(xlUp).Row
    Do While CurrentRowB <= LastRowB
        If Cells(CurrentRowA, "A").Value = Cells(CurrentRowB, "B").Value Then
            Exit Do
        Else
        CurrentRowB = CurrentRowB + 1
        End If
    Loop
    If CurrentRowB > LastRowB Then
        Cells(CurrentRowB, "B").Value = Cells(CurrentRowA, "A").Value
        Set r = Range("A1", "A" & LastRowA)
        Cells(CurrentRowB, "C").Value = Application.CountIf(r, Cells(CurrentRowA, "A").Value)
    End If
    CurrentRowA = CurrentRowA + 1
Wend
LastRowB = Range("B50000").End(xlUp).Row
Range("B2", "C" & LastRowB).Cut
Range("B1").Select
ActiveSheet.Paste

If what i described in my latest comment is what you really want all you have to do is paste this formulas in B1 =COUNTIF($A$1:A1;A1) and drag it to the last cell or double click in that blac square on B1 bottomtight corner, then if the calcution is automatic it's done, if it's manual you have to click calculate now and it's done 如果我在最近的评论中描述的是您真正想要做的就是将该公式粘贴到B1 = COUNTIF($ A $ 1:A1; A1)中,并将其拖到最后一个单元格中,或者双击B1的底角,如果自动计算完成,如果是手动,则必须单击“立即计算”并完成

Hope it helps, Bruno 希望能有所帮助,布鲁诺

Paste this in D1 and drag down. 将此粘贴到D1中并向下拖动。

=IF(A2<>A1,COUNTIF($A$1:$A$100000,A1),"")

Adjust the range as you need. 根据需要调整范围。 This formula assumes that the first 3 digits are in there own cell. 此公式假定前3位数字位于自己的单元格中。

If your sample data is all in one column then you will have to use a Sumproduct with a Left function in place of the countif. 如果您的样本数据全部集中在一列中,那么您将不得不使用带有Left函数的Sumproduct代替countif。 You can use the following Formula in this case, But if your sample data is in 3 columns definatly use my fast formula. 在这种情况下,您可以使用以下公式,但是如果您的示例数据在3列中,则请绝对使用我的快速公式。

=IF(LEFT(A1,3)<>LEFT(A2,3),SUMPRODUCT(--(LEFT($A$1:$A$100000,3)=LEFT(A1,3))),"")

EDIT Based on your comments and answer I have made a full guide on using the countif method as VBA should ALWAYS be avoided if possible. 编辑根据您的意见,并回答我已经做了充分的指导上使用countif方法VBA应该总是尽量避免使用。 You had issues because your sample data provided in your question did not contain headers/ Column Labels here is the fixed guide. 您遇到了问题,因为问题中提供的示例数据不包含标题/列标签,此处为固定指南。

Starting with your 3 columns with headers I wqould create a named range on the column youd like the counts for to do this use built in Name Manager and click on new: 从带有标题的3列开始,我将在名称列上创建一个命名范围,以进行计数,以使用内置在名称管理器中的名称,然后单击new:

姓名经理新

Then from this Set the Name to CountColumn and in the Formula use the following: 然后从此将Name设置为CountColumn并在公式中使用以下内容:

=OFFSET($A$2,0,0,COUNTA($A$2:$A$1000000),1)

名称管理器公式

Now using a modified version of my original answer type the following in cell D2 : 现在使用原始答案的修改版本,在单元格D2输入以下内容:

=IF(A3<>A2,COUNTIF(CountColumn,A2),"")

式

AS shown above this is IDENTICAL to what your original question Asked for in Desired Output . 如上所示,这与您在“ Desired Output要求的原始问题完全相同。

Now to further this to get the highlights as your VBA Code looks to do I would use the following. 现在,随着您的VBA代码的发展,来进一步强调这一点,我将使用以下内容。

Go Back to the Name Manager, as we did for the CountColumn , and Create another new Named Range called Sums And then change all the A references to D like follows: 回到名称管理器,因为我们没有为CountColumn ,并创建另一个新的指定范围称为Sums ,然后更改所有的A引用D喜欢如下:

=OFFSET($D$2,0,0,COUNTA($D$2:$D$1000000),1)

SumsNamed

And you Name Manager Should look like the following: 您的名称管理器应如下所示:

名称管理员2

Now in the Name Box (top left box next to formula bar) type in the word Sums to select the entire sum area so we can format it: 现在,在名称框(位于编辑栏旁边的左上角框)中,输入单词Sums以选择整个求和区域,以便我们对其进行格式化:

总和

Then ****while sums area is highlighted*** go to Conditional Formatting ~~> New Rule: 然后****同时突出显示sums区域***转到条件格式~~>新规则:

在此处输入图片说明 And use the built in No Blanks Function: 并使用内置的No Blanks功能:

没有空白

Then for the format Use Fill and the color you want, Based on your posted formula I used the Green Color: 然后针对“使用填充”格式和所需的颜色,根据您发布的公式,我使用了“绿色”:

填充绿色

Now you should be done and your Data should look as the picture below does: 现在您应该已经完成​​,您的数据应该如下图所示:

完

The following assumes that your data is all in one column (eg: "315 1 344" is one cell) 以下假设您的数据全部在一列中(例如: "315 1 344"是一个单元格)

It will look at sheet1 starting from A1, generate a list of unique cell values and count any duplicates. 它将从A1开始查看sheet1,生成一个唯一单元格值列表并计算所有重复项。 Once all the records have been checked, it outputs the results to sheet2. 检查完所有记录后,它将结果输出到sheet2。

Sub Main()
' this requires you to add a reference to "Microsoft Scripting Runtime" (usefull if you do not know the methods of scripting.dictionary)
'Dim Results As New Scripting.Dictionary
' the line does not require you to add any extra references (there is no code-completion, you must know the methods and their arguments)
Dim Results As Object: Set Results = CreateObject("Scripting.Dictionary")
Dim Data As Variant
Dim Key As Variant
Dim Source As Worksheet: Set Source = ThisWorkbook.Worksheets("Sheet1") ' the sheet where your data is
Dim Destination As Worksheet: Set Destination = ThisWorkbook.Worksheets("Sheet2") ' where the results will be put
Dim Row As Long: Row = 1 ' the row number to start from
Dim Item As String
Data = Source.UsedRange.Value2
' iterate over the data
Do
    Item = Data(Row, 1)
    If Results.Exists(Item) = True Then
        Results(Item) = Results(Item) + 1
    Else
        Results(Item) = 1
    End If
    Row = Row + 1
Loop While Not Data(Row, 1) = ""
' display the output
Destination.Cells.Clear ' reset the worksheet
For Each Key In Results.Keys ' loop through the results
    Destination.Range("A1:B1").Insert xlShiftDown ' move the previous results down
    Destination.Cells(1, 1) = Key
    Destination.Cells(1, 2) = Results(Key)
Next Key

End Sub

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

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