繁体   English   中英

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

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

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

我有这组数据,大约有4000条以我要跟踪的模式发生的记录。 第一栏很重要。 我想浏览该列,并在NEW列中记录该数字出现的人工倍数。 是否可以在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  

13/5/23数据由空格分隔。 并非全部都在一个单元中。 我不知道如何在此处创建网格图片。 最左边的单元格是我要计算的单元格。

所需的输出是我想要的示例。 有314次出现的6个事件,我希望将计数摘要单元格编译到最后一个事件的行中。

我备份,放慢了脚步,并开始学习一些基本的编程原理,这些原理有时会感到有些慢。

  1. 流程图
  2. 伪代码
  3. 原型
  4. 测试
  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)

这里的range是要检查的单元格(根据您的说法,它是“ 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的底角,如果自动计算完成,如果是手动,则必须单击“立即计算”并完成

希望能有所帮助,布鲁诺

将此粘贴到D1中并向下拖动。

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

根据需要调整范围。 此公式假定前3位数字位于自己的单元格中。

如果您的样本数据全部集中在一列中,那么您将不得不使用带有Left函数的Sumproduct代替countif。 在这种情况下,您可以使用以下公式,但是如果您的示例数据在3列中,则请绝对使用我的快速公式。

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

编辑根据您的意见,并回答我已经做了充分的指导上使用countif方法VBA应该总是尽量避免使用。 您遇到了问题,因为问题中提供的示例数据不包含标题/列标签,此处为固定指南。

从带有标题的3列开始,我将在名称列上创建一个命名范围,以进行计数,以使用内置在名称管理器中的名称,然后单击new:

姓名经理新

然后从此将Name设置为CountColumn并在公式中使用以下内容:

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

名称管理器公式

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

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

式

如上所示,这与您在“ Desired Output要求的原始问题完全相同。

现在,随着您的VBA代码的发展,来进一步强调这一点,我将使用以下内容。

回到名称管理器,因为我们没有为CountColumn ,并创建另一个新的指定范围称为Sums ,然后更改所有的A引用D喜欢如下:

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

SumsNamed

您的名称管理器应如下所示:

名称管理员2

现在,在名称框(位于编辑栏旁边的左上角框)中,输入单词Sums以选择整个求和区域,以便我们对其进行格式化:

总和

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

在此处输入图片说明 并使用内置的No Blanks功能:

没有空白

然后针对“使用填充”格式和所需的颜色,根据您发布的公式,我使用了“绿色”:

填充绿色

现在您应该已经完成​​,您的数据应该如下图所示:

完

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

它将从A1开始查看sheet1,生成一个唯一单元格值列表并计算所有重复项。 检查完所有记录后,它将结果输出到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