+ Reply to Thread
Results 1 to 9 of 9

Create unique items in valiadtion list

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Create unique items in valiadtion list

    Hello everyone
    I have a validation list of three items (A,B,C)
    these values are in column A ..
    In E4 I have validation list of the three values (A,B,C)
    The required in F4 to create validation list of unique items for specific item
    As an example if I select A in cell E4 , i expect to have a list of these values(20,5,9,19) only
    Hope it is clear
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Create unique items in valiadtion list

    Hi buddy,

    try this worksheet event (put in the Sheet1 module):
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim a, i As Long, d As Object
        If Target.Cells.Count > 1 Then Exit Sub
        
        With Range("E4")    'dropdown cell
            If Not Intersect(Target, .Cells(1)) Is Nothing Then
            
                a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
                Set d = CreateObject("Scripting.Dictionary")
                
                For i = 1 To UBound(a, 1)
                    If a(i, 1) = .Value Then d.Item(a(i, 2)) = Empty
                Next
                
                If d.Count Then
                    With .Offset(, 1).Validation    'change validation on the cell next to the dropdown
                        .Delete: .Add 3, , , Join(d.keys, ",")
                    End With
                End If
                
                Set d = Nothing
                
            End If
        End With
        
    End Sub
    Hope this helps,

    berlan

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: Create unique items in valiadtion list

    On the Worksheet Module -

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Not Intersect(Target, Range("E4")) Is Nothing Then
            Unique
        End If
        
    End Sub
    On a regular code module -

    Sub Unique()
        Dim r, s, InputVal As String, Counter As Integer, i As Integer, Str As String
        r = Sheet1.Range("A2:B" & Cells(Rows.Count, 1).End(xlUp).Row)
        ReDim s(0)
        
        InputVal = Sheet1.Range("E4").Value2
        
        For Counter = LBound(r, 1) To UBound(r, 1)
            
            If r(Counter, 1) = InputVal Then
                i = i + 1
                ReDim Preserve s(i - 1)
                s(i - 1) = r(Counter, 2)
            End If
            
        Next Counter
                
        For Counter = LBound(s) To UBound(s)
            Str = Str & s(Counter) & ","
        Next Counter
        
        With Sheet1.Range("F4").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Left(Str, Len(Str) - 1)
            .IgnoreBlank = True
            .InCellDropdown = True
        End With
        
    End Sub
    See the file attached for reference.
    Attached Files Attached Files
    Cheers!
    Deep Dave

  4. #4
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,879

    Re: Create unique items in valiadtion list

    After seeing Berlan's code, I just realized I missed out on the "Unique" Part..

    Sorry!
    Last edited by NeedForExcel; 08-10-2015 at 06:55 AM.

  5. #5
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: Create unique items in valiadtion list

    another way of doing it:


    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim uniq, Rng As Range
        
        If Target.Address <> "$E$4" Then Exit Sub
        
        Range("K3:L" & Range("L" & Rows.Count).End(xlUp).Row).Clear
        Range("F4").ClearContents
        
        Range("A1:B" & Range("A" & Rows.Count).End(xlUp).Row).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
        "E3:F4"), CopyToRange:=Range("K3:L3"), Unique:=True
        Set Rng = Range("L4:L" & Range("L" & Rows.Count).End(xlUp).Row)
        ActiveWorkbook.Names.Add Name:="uniq", RefersTo:=Rng
        
        With Range("F4").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="=uniq"
            
        End With
    End Sub
    Attached Files Attached Files
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create unique items in valiadtion list

    Thanks a lot everyone for these great solutions
    @NeedForExcel it is ok but for it brings all the values .. I refer in my request to unique values
    @bulina2k very good solution but its problem it depends on columns inside the sheet and creating named ranges ..
    @ berlan yout code is perfect for me frankly ..

    Is it possible to create a dynamic list (unique results) by formulas ? just for curious
    Thank you very much for you great help

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create unique items in valiadtion list

    @Mr Berlan
    How to deal with the following attachment
    I need to create a nother validation list based on F4 in cell G4
    Sorry for bothering you again
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Create unique items in valiadtion list

    Thanks again for kind feedback. Taking a third validation list into account, this should do the trick:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim a, i As Long
    
        If Target.Cells.Count > 1 Then Exit Sub
        If Target.Value = vbNullString Then Exit Sub
        If Intersect(Range("E4:F4"), Target) Is Nothing Then Exit Sub
    
        a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value
    
        With CreateObject("Scripting.Dictionary")
            For i = 1 To UBound(a, 1)
                If Target.Address = "$E$4" Then
                    If a(i, 1) = Target.Value Then .Item(a(i, 2)) = Empty
                Else
                    If a(i, 1) = Target(, 0).Value And a(i, 2) = Target.Value Then .Item(a(i, 3)) = Empty
                End If
            Next
            If .Count Then
                Target.Resize(, 2).Offset(, 1).Validation.Delete
                Target.Offset(, 1).Validation.Add 3, , , Join(.keys, ",")
                Application.EnableEvents = False
                Target.Offset(, 1).ClearContents
                Application.EnableEvents = True
            End If
        End With
    
    End Sub
    Cheers
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create unique items in valiadtion list

    You are awesome ..Thank you very much for this wonderful solution

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] formula to create list of unique items
    By pink in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 12-13-2014, 02:29 AM
  2. [SOLVED] Limitation on formula to create list of unique items
    By cman0 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-23-2014, 11:44 AM
  3. [SOLVED] Unique List of Items
    By Shirayuki in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-24-2013, 11:06 AM
  4. [SOLVED] create list of unique staff numbers with unique work codes
    By shaz0503 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-01-2013, 08:57 PM
  5. [SOLVED] Counting # of Unique Items In List & Listing Unique Items In A List
    By unpluggedmusic in forum Excel General
    Replies: 3
    Last Post: 12-05-2012, 11:44 AM
  6. [SOLVED] tagging unique items in a list
    By K. Gwynn in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-16-2006, 09:25 AM
  7. create an array with unique items IN MEMORY
    By Werner Rohrmoser in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-25-2005, 10:05 AM
  8. returning a list of unique items
    By Buska in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-29-2005, 07:59 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1