Add Unique items

Digitborn.com

Active Member
Joined
Apr 3, 2007
Messages
353
Hello,

I have a code which works perfectly and adds the dates from Column A to UserForm1 ComboBox2.
In column B, I have textdata - UserForm1 ComboBox5.

I'd like to transform the code to add only the dates which correspond (cascading) to 1.1 textdata in ComboBox5 (Column B) and 1.2 dates in ComboBox2 (Column A).
Code:
    ElseIf ComboBox5.ListIndex <> 0 Then
        ComboBox2.Style = fmStyleDropDownList
        ComboBox2.Enabled = True
        ComboBox2.SetFocus
        For Each r In Worksheets("PartsData").Range("A2:A59")
            If r.Offset(, 1).Value = Trim(ComboBox5.Value) Then
                Dim dic As Object, e, d, a
                    Set dic = CreateObject("Scripting.Dictionary")
                    dic.CompareMode = vbTextCompare
                    With Sheets("PartsData")
                        With .Range("A2:AD" & .Range("A" & Rows.Count).End(xlUp).Row)
                            .Sort .Range("A2"), xlAscending
                            a = .Offset(0, 0).Resize(, 1).Value
                        End With
                    End With
                    For Each e In a
                        If Not IsEmpty(e) Then
                            d = Format(e, "dd-mmm-yy")
                        If Not dic.exists(d) Then dic.Add d, Nothing
                        End If
                    Next
                        Me.ComboBox2.List = dic.keys
                    Set dic = Nothing: Erase a
            End If
        Next
    End If
Normaly I know how to do it -> ComboBox2.AddItem = r.Offset(, 1).Value but in this case Me.ComboBox2.List = dic.keys is unknown for me. All code for ComboBox2 is written because of the dates format and sort.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Kris helP,


I've changed my PartsData worksheet's structure, adding as Column A - ID. And now I can't fix your code to work properly. The problem is with the Sorting. The sorting criteria is Column B (where are the dates), It should sort A2:BM & .Range("B" & Rows.Count).End(xlUp).Row).

The code below, sorts the dates by Column B, but sorts B2:BM & .Range("B" & Rows.Count).End(xlUp).Row) and don't sort Column A (IDs). Whatever I try to change it doesn't work, don't know why :-? .

Here's the full code for the ComboBox control:
Code:
Private Sub ComboBox2P2_Change()
If ComboBox2P2.ListIndex <> 0 And ComboBox2P2.ListIndex <> -1 Then
        With Worksheets("PartsData")
            If Len(Me.ComboBox2P2) > 0 Then
                Dim dic As Object, i As Long, d, a, z, Y
                z = Me.ComboBox2P2.Value
                Set dic = CreateObject("Scripting.Dictionary")
                dic.CompareMode = vbTextCompare
                    With .Range("B2:BM" & .Range("B" & Rows.Count).End(xlUp).Row)
                        .Sort .Range("A2"), xlAscending
                        a = .Offset(0, 0).Resize(, 2).Value
                    End With
                For i = 1 To UBound(a, 1)
                    If Not IsEmpty(a(i, 1)) Then
                        d = Format(a(i, 1), "dd-mmm-yy")
                        If Not dic.exists(d) And a(i, 2) = z Then dic.Add d, Nothing
                    End If
                Next: Y = dic.keys: Set dic = Nothing: Erase a
            End If
        End With
    End If
End Sub

p.s. If I change this line:
Code:
With .Range("B2:BM" & .Range("B" & Rows.Count).End(xlUp).Row)
to
Code:
With .Range("A2:BM" & .Range("B" & Rows.Count).End(xlUp).Row)
and trigger Event ComboBox2_Change, nothing's happening on the UserForm.
 
Upvote 0
Hi,

Replace

Code:
With .Range("B2:BM" & .Range("B" & Rows.Count).End(xlUp).Row)
    .Sort .Range("A2"), xlAscending
    a = .Offset(0, 0).Resize(, 2).Value
End With

with

Code:
With .Range("A2:BM" & .Range("B" & Rows.Count).End(xlUp).Row)
    .Sort .Range("B2"), xlAscending
    a = .Offset(0, 1).Resize(, 2).Value
End With

HTH
 
Upvote 0

Forum statistics

Threads
1,223,838
Messages
6,174,937
Members
452,593
Latest member
Jason5710

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top