Private Sub CommandButton1_Click()
Dim dict As Object, rCell As Range
Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
With Sheets("Template")
For Each rCell In .Range("D7:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
dict.Item(rCell.Value) = Empty
Next rCell
End With
For Each V In dict.Keys()
With Sheets("Header")
.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = V
.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = V
.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Value = "Test"
.Cells(Rows.Count, 7).End(xlUp).Offset(1, 0).Value = Sheets("Template").Range("E" & .Cells(Rows))
End With
Next V
End Sub
How can i do that? '// Pair class
Option Explicit
Private mFirst As Variant
Private mSecond As Variant
Friend Sub Construct(ByVal First As Variant, ByVal Second As Variant)
mFirst = First
mSecond = Second
End Sub
Public Property Get First() As Variant
First = mFirst
End Property
Public Property Get Second() As Variant
Second = mSecond
End Property
Second, a Factory module with an helper function to instanciate a Pair: '// Factory module
Option Explicit
Public Function Create_Pair(ByVal First As Variant, ByVal Second As Variant) As Pair
Set Create_Pair = New Pair
Create_Pair.Construct First, Second
End Function
Finally, the event handler attached to your button as well as a second helper function:Option Explicit
Private Sub CommandButton1_Click()
Dim Uniques As Collection
Set Uniques = GetUniques
Dim Wb As Excel.Workbook
Set Wb = ThisWorkbook
Dim Ws As Excel.Worksheet
Set Ws = Wb.Worksheets("Header")
Dim Rng As Excel.Range
Set Rng = Ws.Range("A2")
Dim Pair As Pair
For Each Pair In Uniques
Rng.Value = Pair.First
Rng.Offset(ColumnOffset:=1).Value = Pair.First
Rng.Offset(ColumnOffset:=6).Value = Pair.Second
Set Rng = Rng.Offset(RowOffset:=1)
Next
End Sub
'// Build a collection of uniques items
Private Function GetUniques() As Collection
Dim Uniques As Collection
Set Uniques = New Collection
Dim Rng As Excel.Range
For Each Rng In Me.Range("D7:D" & Me.Cells(Me.Rows.Count, "D").End(xlUp).Row)
Dim Pair As Pair
Set Pair = Factory.Create_Pair(Rng.Value, Rng.Offset(ColumnOffset:=1).Value)
On Error Resume Next
'// attempt to insert a pair in the collection
'// if it fail, ignore and move to the next
Uniques.Add Pair, CStr(Pair.First)
On Error GoTo 0
Next
Set GetUniques = Uniques
End Function
Hi Fabrice, Would you know why i'M getting this below error?You did not rename the required Pair class module, neither the required Factory module....
Open in new window