Option Explicit
Sub dictionaryexample()
Sheets("conditionalformattingvba").Select
Dim dict As Object 'Declare a generic Object reference
Set dict = CreateObject("Scripting.Dictionary") 'Late Binding of the Dictionary
Range("T2").Select
While ActiveCell.Value <> ""
Dim key, val
key = ActiveCell.Value: val = ActiveCell.Offset(0, 1).Address
'Add item to VBA Dictionary
If Not dict.Exists(key) Then
dict.Add key, val
End If
ActiveCell.Offset(1, 0).Select
Wend
Debug.Print dict.Count 'Result: 1
For Each key In dict.Keys
Debug.Print key
Next key
'Print all items
For Each val In dict.Items
Debug.Print val
Next val
'copy format of cells to different location
Dim tempaddress As String
Range("V2").Select
tempaddress = Selection.Address
For Each val In dict.Items
Range(val).Select
Selection.Copy
Range(tempaddress).PasteSpecial Paste:=xlPasteFormats
tempaddress = ActiveCell.Offset(1, 0).Address
Next val
Application.CutCopyMode = False
'Dispose of VBA Dictionary
Set dict = Nothing
End Sub
Sub dictionaryexample()
Sheets("conditionalformattingvba").Select
Dim dict As Object 'Declare a generic Object reference
Set dict = CreateObject("Scripting.Dictionary") 'Late Binding of the Dictionary
Range("T2").Select
While ActiveCell.Value <> ""
Dim key, val
key = ActiveCell.Value: val = ActiveCell.Offset(0, 1).Address
'Add item to VBA Dictionary
If Not dict.Exists(key) Then
dict.Add key, val
End If
ActiveCell.Offset(1, 0).Select
Wend
Debug.Print dict.Count 'Result: 1
For Each key In dict.Keys
Debug.Print key
Next key
'Print all items
For Each val In dict.Items
Debug.Print val
Next val
'copy format of cells to different location
Dim tempaddress As String
Range("V2").Select
tempaddress = Selection.Address
For Each val In dict.Items
Range(val).Select
Selection.Copy
Range(tempaddress).PasteSpecial Paste:=xlPasteFormats
tempaddress = ActiveCell.Offset(1, 0).Address
Next val
Application.CutCopyMode = False
'Dispose of VBA Dictionary
Set dict = Nothing
End Sub
No comments:
Post a Comment