20170324xlVBA最简单分类计数

Posted Excel VBA 小天地

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20170324xlVBA最简单分类计数相关的知识,希望对你有一定的参考价值。

Sub NextSeven_CodeFrame()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    Application.StatusBar = ">>>>>>>>程序正在运行>>>>>>>>"


    ‘On Error GoTo ErrHandler

    Dim StartTime, UsedTime As Variant
    StartTime = VBA.Timer
    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Dim Dic As Object
Set Dic = CreateObject("Scripting.Dictionary")
    
    Dim Wb As Workbook
    Dim Sht As Worksheet
    Dim oSht As Worksheet
    Dim Rng As Range
    Dim Arr As Variant
    Dim EndRow As Long
    Const HEAD_ROW As Long = 1
    Const SHEET_NAME As String = "数据"
    Const START_COLUMN As String = "A"
    Const END_COLUMN As String = "Z"

    Const OTHER_HEAD_ROW As Long = 1
    Const OTHER_SHEET_NAME As String = "重复"

    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    Set Wb = Application.ThisWorkbook
    Set Sht = Wb.Worksheets(SHEET_NAME)
    With Sht
        EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
        Set Rng = .Range(.Cells(HEAD_ROW + 1, START_COLUMN), .Cells(EndRow, END_COLUMN))

        Arr = Rng.Value
        For i = LBound(Arr) To UBound(Arr)
            
            Key = "‘" & CStr(Arr(i, 4))
            If Key <> "‘" Then
            Dic(Key) = Dic(Key) + 1
            End If
        Next i
    End With
    
     ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     For Each onekey In Dic.KEYS
        If Dic(onekey) < 2 Then
            Dic.Remove onekey
        End If
     Next onekey
     
    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    Set oSht = Wb.Worksheets(OTHER_SHEET_NAME)
    With oSht
       .Cells.ClearContents
       .Range("A1:B1").Value = Array("ID", "次数")
       If Dic.Count > 0 Then
        .Range("A2").Resize(Dic.Count, 2).Value = Application.WorksheetFunction.Transpose(Array(Dic.KEYS, Dic.ITEMS))
       End If
    End With


    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    UsedTime = VBA.Timer - StartTime
    MsgBox "本次耗时:" & Format(UsedTime, "0.000秒"), vbOKOnly, "NextSeven Excel Studio"

ErrorExit:
      Set Wb = Nothing
      Set Sht = Nothing
      Set Rng = Nothing
      Set oSht = Nothing
      Set Dic = Nothing

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.Calculation = xlCalculationAutomatic
    Application.StatusBar = False
    Exit Sub
    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ErrHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Description & "!", vbCritical, "NextSeven Excel Studio"
        ‘Debug.Print Err.Description
        Err.Clear
        Resume ErrorExit
    End If
End Sub

  

以上是关于20170324xlVBA最简单分类计数的主要内容,如果未能解决你的问题,请参考以下文章

20170617xlVBA销售数据分类汇总

20170711xlVBA自定义分类汇总一例

20170624xlVBA正则分割分类汇总

20170928xlVBA自定义分类汇总

20170814xlVBA限定日期按客户分类汇总

20170612xlVBA多文件多类别分类求和匹配