字典的計(jì)數(shù)/求和都是通過(guò)利用,字典keys鍵的唯一性進(jìn)行統(tǒng)計(jì),當(dāng)key鍵出現(xiàn)相同時(shí)進(jìn)行數(shù)量的加一或者進(jìn)行keys鍵的值求和! Sub RES(ByVal ST As Integer) Dim arr, i, brr, dic '定義變量 Set dic = CreateObject('scripting.dictionary') '創(chuàng)建字典對(duì)象 arr = [A2:B29] '把A2至B29的數(shù)據(jù)裝入數(shù)組arr For i = 1 To UBound(arr) '遍歷數(shù)據(jù)取數(shù) If ST = 1 Then '狀態(tài)當(dāng)為1時(shí)執(zhí)行 value統(tǒng)計(jì)計(jì)數(shù)否則執(zhí)行value求和 dic(arr(i, 1)) = dic(arr(i, 1)) 1 '執(zhí)行keys計(jì)數(shù) Else dic(arr(i, 1)) = dic(arr(i, 1)) arr(i, 2) '執(zhí)行keys的value求和 End If Next brr = Array(dic.keys, dic.items) '把統(tǒng)計(jì)后的數(shù)據(jù)裝入數(shù)組brr [D2].Resize(dic.Count, 2) = WorksheetFunction.Transpose(brr) '把brr數(shù)組寫(xiě)入單元格 End Sub
Sub A_SUM() '執(zhí)行keys的value求和 RES (0) End Sub
Sub A_COUNT() '執(zhí)行keys計(jì)數(shù) RES (1) End Sub
文件連接: 鏈接:https://pan.baidu.com/s/1lfJ7Q2zf76RGRahXb9_y-w 提取碼:jrsn --------------------------------------------------------------------------- --------------------------------------------------------------------------- ***********************************************************************
|
|