VBA - Dictionary(Late vs Early Binding) by 우노사설
Dictionary방식( Late Binding & Early Binding)
1. Late Binding : 'MicroSoft Scripting Runtime'체크확인 불필요
Sub usedictionary()
Dim rDatas As Range
Dim oDic As Object '일단 범용 Object로 선언 후
Dim rRow As Range
Dim sKey As String
Dim iItem As Integer
Dim ix As Integer
Set rDatas = Worksheets("Sales").Range("A1").CurrentRegion
Set rDatas = rDatas.Offset(1).Resize(rDatas.Rows.Count - 1)
Set oDic = CreateObject("scripting.Dictionary") 'Dictionary로 지정한다.
For Each rRow In rDatas.Rows
sKey = rRow.Cells(3)
iItem = rRow.Cells(4)
If Not oDic.exists(sKey) Then
oDic.Add sKey, iItem
Else
oDic(sKey) = oDic(sKey) + iItem
End If
Next
With Range("H1")
For ix = 0 To oDic.Count - 1
.Offset(ix) = oDic.keys()(ix) ()() 반드시 지켜야 한다.
.Offset(ix, 1) = oDic.items()(ix) ()() 반드시 지켜야 한다.
Next
End With
End Sub
https://www.youtube.com/watch?v=wKbOZpZ9vVQ
2. Early Binding : 'MicroSoft Scripting Runtime'체크확인 필요
Sub usedictionary2()
Dim rDatas As Range
Dim oDic As Scripting.Dictionary
Dim rRow As Range
Dim sKey As String
Dim iItem As Integer
Dim ix As Integer
Set rDatas = Worksheets("Sales").Range("A1").CurrentRegion
Set rDatas = rDatas.Offset(1).Resize(rDatas.Rows.Count - 1)
Set oDic = New Dictionary
For Each rRow In rDatas.Rows
sKey = rRow.Cells(3)
iItem = rRow.Cells(4)
If Not oDic.exists(sKey) Then
oDic.Add sKey, iItem
Else
oDic(sKey) = oDic(sKey) + iItem
End If
Next
With Range("H1")
For ix = 0 To oDic.Count - 1
.Offset(ix) = oDic.keys(ix) ()() or () 둘 중 하나 사용해도 된다.
.Offset(ix, 1) = oDic.Items(ix) ()() or () 둘 중 하나 사용해도 된다.
Next
End With
End Sub
https://www.youtube.com/watch?v=aFKrdTGgUT8
https://raymond.tistory.com/2246
엑셀 VBA #118 / Dictionary 개체_기본 [VBA]