+ Reply to Thread
Results 1 to 8 of 8

Thread: Combobox alternatives based on a range

  1. #1
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Combobox alternatives based on a range

    Hi,

    I have a simple but still very annoying problem...

    I have a list of persons, as in my dummybook. The list can contain any
    number of personons. I want to put all of the dates in a combobox as
    alternatives, but only one of each and no blank alternatives.

    So the combobox should contain unique values and no blank values!

    The combobox should also uppdate itself when a new post is added.

    How do I get this to work ?

    Many thanks!!

    //

    Per
    Attached Files Attached Files
    Last edited by Pero; 03-09-2011 at 08:24 AM.

  2. #2
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Combobox alternatives based on a range

    Hi Pero

    One way is to create a dynamic named range of the dates. Advanced Filter the date range (including the header) to sheet2 then name that range. See attached.

    Sorry, I left a piece of code out...I'll repost the workbook.
    Attached Files Attached Files
    Last edited by jaslake; 03-06-2011 at 04:42 PM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Combobox alternatives based on a range

    Hi,

    thanks for reply!

    It works but is there a way of doing this whithout copying the filter result to a new sheet and then add them to a combobox ? w
    It must be possible to write a VBA-code that adds all unique dates to a array and then add the array to the combobox or something ?
    Without any visual interaction!

    Thanks again!

  4. #4
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Combobox alternatives based on a range

    Hi Pero

    Actually, there is a way to do this
    It must be possible to write a VBA-code that adds all unique dates to a array
    and I worked with something like that recently. I'll need to dig through my scrap heap and see if I can give you an example.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Combobox alternatives based on a range

    Hi Pero

    Notice this Requires, Tools > Reference > Microsoft Scripting Runtime, scrrun.dll
    Place this code in a general module
    Option Explicit
    Sub Test_UniqueArray()
        Dim a As Variant
        Dim lr As Long
        Dim i As Long
        lr = Range("N" & Rows.Count).End(xlUp).Row
        a = Worksheets("Sheet1").Range("N7:N" & lr).Value
        a = UniqueArray(a)
        Sheet1.ComboBox1.List = WorksheetFunction.Transpose(a)
    End Sub
     
     
    Function UniqueArray(anArray As Variant) As Variant
    'Requires, Tools > Reference > Microsoft Scripting Runtime, scrrun.dll
        Dim d As New Scripting.Dictionary, a As Variant
        With d
            .CompareMode = TextCompare
            For Each a In anArray
                If Not Len(a) = 0 And Not .Exists(a) Then
                    .Add a, Nothing
                End If
            Next a
            UniqueArray = d.keys
        End With
        Set d = Nothing
    End Function
    Place this code in Sheet1
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim lr As Long
        lr = Range("N" & Rows.Count).End(xlUp).Row
        If Not Intersect(Target, Range("N7:N" & lr)) Is Nothing Then
            Call Test_UniqueArray
        End If
    End Sub
    Let me know of issues.
    Last edited by jaslake; 03-06-2011 at 06:31 PM. Reason: Corrected Sheet Change Event Code
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  6. #6
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Combobox alternatives based on a range

    Hi, Thanks ! Almost works..

    If the list only contains one personi get an error "Type missmatch" on
    line:
    For Each a In anArray
    That must be because anArray is difined as a variant and when it only contains one element variant will choose "wrong" datatype for "anArray" and "For Each a in anArray" wont work.

    I'm I right ?


    EDIT:

    Also , the dates in the combobox are displayed in another format then in the actual list. The list contains dates in format YYYY-MM-DD but in the combobox list its : MM-DD-YYYY
    I want it to be YYYY-MM-DD in the combobox aswell!

    Thanks again !
    Last edited by Pero; 03-07-2011 at 11:15 AM.

  7. #7
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Combobox alternatives based on a range

    Hi Pero

    Modify the code as follows
    Option Explicit
    Sub Test_UniqueArray()
        Dim a As Variant
        Dim lr As Long
        Dim i As Long
        lr = Range("N" & Rows.Count).End(xlUp).Row
        a = Worksheets("Sheet1").Range("N7:N" & lr).Value
     
        Sheet1.ComboBox1.Clear
      If lr = 7 Then
          Sheet1.ComboBox1.AddItem a
      Else
            a = UniqueArray(a)
            Sheet1.ComboBox1.List = WorksheetFunction.Transpose(a)
        End If
    End Sub
     
     
    Function UniqueArray(anArray As Variant) As Variant
    'Requires, Tools > Reference > Microsoft Scripting Runtime, scrrun.dll
        Dim d As New Scripting.Dictionary, a As Variant
        With d
            .CompareMode = TextCompare
            For Each a In anArray
                If Not Len(a) = 0 And Not .Exists(a) Then
                    .Add a, Nothing
                End If
            Next a
            UniqueArray = d.keys
        End With
        Set d = Nothing
    End Function
    Let me know of issues.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  8. #8
    Registered User
    Join Date
    08-20-2009
    Location
    Stockholm,Sweden
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Combobox alternatives based on a range

    It works =) .
    Thnaks you very much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0