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
Last edited by Pero; 03-09-2011 at 08:24 AM.
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.
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.
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!
Hi Pero
Actually, there is a way to do thisand 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.It must be possible to write a VBA-code that adds all unique dates to a array
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.
Hi Pero
Notice this Requires, Tools > Reference > Microsoft Scripting Runtime, scrrun.dll
Place this code in a general modulePlace this code in Sheet1Option 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 FunctionLet me know of issues.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
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.
Hi, Thanks ! Almost works..
If the list only contains one personi get an error "Type missmatch" on
line:
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.For Each a In anArray
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.
Hi Pero
Modify the code as followsLet me know of issues.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
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.
It works =) .
Thnaks you very much!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks