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

我手动扫描90条logging后意识到,除非我使用自动化,否则这将是痛苦和乏味的。

我有这套数据,大约有4000个logging,这些logging是以我想追踪的模式发生的。 第一列是重要的一列。 我想扫描整个列,并在新列中logging该人数是多less倍。 是否有可能在Excel中以编程方式执行此操作?

注意:我不只是寻找一个模式或一个模式的单一事件。

例如在这个样本313中发生1次,314发生6次,315发生2次等等

在事件结束时,我希望它看起来像

--- 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数据由空格分隔。 这不是全部在一个单元格。 我不知道如何在这里创build一个网格图片。 最左边的单元格是我要计算的单元格。

所需的输出是我想要的例子。 有314个事件发生了6次,我希望计数摘要单元格被编译在最后一次出现的行中。

我支持,慢下来,并遵循一些基本的编程原则,有时他们觉得很慢。

  1. stream程图
  2. 伪代码
  3. 原型
  4. testing
  5. 根据需要重复3和4。

我发现下面的代码完全是我需要的。 我跟谁分享。

 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 

如果你想要做的就是统计一定范围内特定数量的出现次数,那么你只需要使用COUNTIF(range,criteria)

其中范围是你想要检查的单元格(按照你的意思是“A1:A4000”),标准是你正在寻找的数量,它也可以是一个像“55”值大于55。

希望它有帮助,布鲁诺

我在评论中提到的代码:

 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 

如果我在我的最新评论中描述的是你真正想要的,你所要做的就是在B1 = COUNTIF($ A $ 1:A1; A1)中粘贴这个公式并拖动到最后一个单元格,或者双击那个双正方形B1底部的angular落,那么如果计算是自动完成的话,如果是手动的,你必须点击计算,现在就完成了

希望它有帮助,布鲁诺

以下假设您的数据全部在一列(例如: "315 1 344"是一个单元格)

它将从A1开始查看sheet1,生成唯一单元格值列表并计算任何重复项。 一旦所有的logging被检查,它将结果输出到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 

将其粘贴到D1并向下拖动。

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

根据需要调整范围。 这个公式假设前3位数字在自己的单元格中。

如果您的样本数据全部在一列中,那么您将不得不使用具有Leftfunction的Sumproduct来代替countif。 在这种情况下,您可以使用下面的公式,但是如果您的示例数据在3列中明确使用我的快速公式。

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

编辑根据您的意见和答案我已经做了一个使用countif方法的完整指南,因为VBA应该总是尽可能避免。 你有问题,因为你的问题提供的示例数据不包含标题/列标签这里是固定的指南。

从你的3个标题栏开始,我将在列上创build一个命名范围,就像在Name Manager中build立的用法一样,点击new:

名称管理器新建

然后从这个名字设置为CountColumn并在公式中使用以下内容:

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

名称经理公式

现在使用我的原始答案的修改版本在单元格D2键入以下内容:

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

式

如上所示,这与你Desired Output要求的原始问题是相同的。

现在进一步,以获得你的VBA代码看起来要做的亮点,我会使用以下。

返回到名称pipe理器,就像我们为CountColumn所做的CountColumn ,然后创build另一个名为CountColumn新命名范围然后将所有的A引用改为D ,如下所示:

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

SumsNamed

而你的名字pipe理器应该如下所示:

名称经理2

现在在名称框(公式栏旁边的左上angular框)中input单词总和,以select整个总和区域,以便我们可以对其进行格式化:

总和

然后****当sums区域突出显示***转到条件格式化~~>新规则:

在这里输入图像说明 并使用内置的无空白function:

没有空白

然后为格式使用填充和您想要的颜色,基于您张贴的公式我使用了绿色:

填绿色

现在你应该完成了,你的数据应该看起来如下图所示:

完