.本文轉(zhuǎn)載自公眾號(hào):哆哆數(shù)學(xué),原創(chuàng)作者:fs哆哆老師。本文著作權(quán)歸原創(chuàng)作者所有,本人收藏此文僅作為學(xué)習(xí)之用,不作其他目的,如有侵權(quán),請(qǐng)聯(lián)系我刪除。 ====字典綁定=== Sub 前期綁定() Dim dic As New Dictionary End Sub sub 后期綁定() Dim dic Set dic= CreateObject('Scripting.Dictionary') End Sub ===字典的6個(gè)方法4個(gè)屬性=== dic.Add '添加關(guān)鍵詞,方法 dic.CompareMode = 1'不區(qū)分大小寫(xiě),如果等于0區(qū)分大小寫(xiě) dic.Count '數(shù)字典里的關(guān)鍵詞有多少個(gè) dic.Exists '判斷關(guān)鍵詞在字典里是否存在 dic.Item '是指條目 dic.Key '是指關(guān)鍵詞 dic.Items '可以返回所有條目的集合,也可以說(shuō)返回一個(gè)從0開(kāi)始編號(hào)的一維數(shù)組,是方法,大家不要理解為屬性,不能當(dāng)作對(duì)象 dic.Keys '可以返回所有的關(guān)鍵字詞集合,也可以說(shuō)返回一個(gè)從0開(kāi)始編號(hào)的一維數(shù)組,也是方法 dic.Remove '清除某一個(gè)關(guān)鍵詞 dic.RemoveAll '清除全部關(guān)鍵詞,而數(shù)組只能清除數(shù)組的值,但不是不能清數(shù)組空間結(jié)構(gòu) . 【問(wèn)題】一個(gè)級(jí)的成績(jī),我想按班別拆分為各個(gè)班的成績(jī)各一個(gè)工作表 Sub 字典拆分() Dim active_sht As Worksheet, rng As Range Set dic = CreateObject('scripting.dictionary') title_row = 2 f_col = 4 Set active_sht = Worksheets('匯總') With active_sht endRow = .Cells.Find('*', .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious).Row '計(jì)算最后一個(gè)工作表的非空行號(hào) endCol = .Cells.Find('*', .Cells(1, 1), xlValues, xlWhole, xlByColumns, xlPrevious).Column '計(jì)算最后一個(gè)工作表的非空列號(hào) ' MsgBox '行:' & endRow & Chr(10) & '列:' & endCol 'Debug.Print arr = .Range(.Cells(1, f_col), .Cells(endRow, f_col)) For i = title_row 1 To UBound(arr) If Not dic.exists(arr(i, 1)) Then Set dic(arr(i, 1)) = Union(.Range(.Cells(1, 1), .Cells(title_row, endCol)), .Cells(i, 1).Resize(1, endCol)) Else Set dic(arr(i, 1)) = Union(dic(arr(i, 1)), .Cells(i, 1).Resize(1, endCol)) End If 'MsgBox i Next i End With With Worksheets('Sheet2') .Range('a1').Resize(dic.Count, 1) = Application.Transpose(dic.keys) End With For j = 0 To dic.Count - 1 Worksheets.Add after:=Worksheets(Sheets.Count) ActiveSheet.Name = dic.keys()(j) With ActiveSheet dic.items()(j).Copy .[a1] End With Next j End Sub . . =====今天再一次練習(xí)一下字典的用法==== |
|