如何计算列/表中的项目总数

时间:2014-11-20 00:19:03

标签: excel macos vba excel-vba

我正在运行osx 10.9和Excel:Mac2011。我需要计算Excel电子表格中每个项目的数量。

例如:

Cat
Fish
Cat
Dog
Cat
Dog

我正在尝试获得一些看起来像这样的输出

Cat    Cat =3
Fish   Dog =2
Cat    Fish =1
Dog
Cat
Dog

输出不需要排序/排序。先来先计算/列出是好的,但我可以根据需要对数据进行排序。(或者更容易)

如果我能提供更多信息来帮助您,请告诉我。

2 个答案:

答案 0 :(得分:0)

您可以遍历所有工作表,为每个工作表创建一个数据透视表,然后将每个数据透视表中的数据复制并粘贴到源工作表上。这是一种奇怪的方式,但它会起作用。这是代码:

Option Explicit
Sub PivotTableCreator()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False 'Done so excel will delete the pivot table sheet without displaying a messagebox asking you if you are sure
    Dim i, WSCount, LastRow As Long
    WSCount = ActiveWorkbook.Worksheets.Count
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    For i = 1 To WSCount
        Worksheets(i).Activate
        LastRow = Range("A1000000").End(xlUp).Row
        Debug.Print LastRow
        Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Range("A1", "A" & LastRow)) 'data from column A is used to create the pivot table
        Worksheets.Add 'new worksheet created for pivottable
        Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, TableDestination:=Range("A1"))
        With PT
            .PivotFields("Animals").Orientation = xlRowField 'Place whatever column A's header is here (Animals is a placeholder)
            .PivotFields("Animals").Orientation = xlDataField
            .DisplayFieldCaptions = False
        End With
        ActiveCell.CurrentRegion.Copy
        Worksheets(ActiveSheet.Index + 1).Range("B2").PasteSpecial Paste:=xlPasteValues 'Paste results where you want to, I use B2 in this example
        ActiveSheet.Delete 'No longer need the pivot table, so this sheet is deleted
        Next
End Sub

答案 1 :(得分:0)

首先也是最简单的,如果你知道列中没有很多不同的值,你可以使用countif():

= COUNTIF(A1:A6," Cat")

否则,如果您在列中有大量不同的项目并且您想要一个自动化解决方案,那么扫描列的VBA例程会计算每个项目的计数,并将这些计数存入其他列似乎是合理的

Sub CountAll()
    Dim searchCol, itemsCol, countCol, sheetName As String
    Dim i, j, startRow As Integer
    Dim aCounts() As Variant
    Dim bAdded, bFound As Boolean

    startRow = 1
    searchCol = "A"
    itemsCol = "B"
    countCol = "C"
    sheetName = "Sheet1"

    ReDim aCounts(2, 1)

    With Sheets(sheetName)
        For i = 1 To .Range(searchCol & startRow).End(xlDown).Row
            For j = 1 To UBound(aCounts, 2)
                If (.Range(searchCol & i).Value) = aCounts(0, j) Then
                    bFound = True
                    Exit For
                Else
                    bFound = False
                End If
            Next
            If (bFound) Then
                aCounts(1, j) = aCounts(1, j) + 1
            Else
                If (aCounts(1, UBound(aCounts, 2)) <> "") Then
                    ReDim Preserve aCounts(2, UBound(aCounts, 2) + 1)
                End If
                aCounts(0, UBound(aCounts, 2)) = .Range(searchCol & i).Value
                aCounts(1, UBound(aCounts, 2)) = 1
            End If
        Next

        i = 1
        For i = 1 To UBound(aCounts, 2)
            .Range(itemsCol & i).Value = aCounts(0, i)
            .Range(countCol & i).Value = aCounts(1, i)
        Next
    End With
End Sub

如果您的所有工作表都相似而且您希望它在每张工作表上运行,只需将With Sheets(sheetName)更改为For Each Sheet in Sheets,将End With更改为Next,然后添加{{ 1}}在每个Sheet之前,并在每次迭代时重置数组:

.Range