Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 188
Default Calling (Function?) from Userform_Initialize to generate list of unique values for Combo box


Hi All,

I would like to be able to call a (function?) from the
userform_initialize sub of a userform to populate a combo
with unique items.

I could do that separately in each userform, but since I have quite a
few that need to do it, I thought I would put the code in a separate
sub and just call it each time.

I am using a slightly modified sub I got from j-walk.com
(http://j-walk.com/ss/excel/tips/tip47.htm) - see below bottom
(basically just changed the variable names).

However, I am not sure how to call this from each userform_initialize
sub, return the list of unique items and use that list to populate the
combo.

Part of my problem is how (where?) to declare the variables so that
they exist in each place I need them and how to pass the unique list
back from the sub / function to the initialize code.

Hope that makes sense - feel free to ask for clarification!

Thanks,

Alan.



+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+


Option Explicit
' This example is based on a tip by J.G. Hussey,
' published in "Visual Basic Programmer's Journal"


Function RemoveDuplicates()

Dim AllCells As Range, Cell As Range
Dim Brands_Unique As New Collection
Dim i As Integer, j As Integer
Dim Swap1, Swap2, Item

' The items are in the Brands_Existing range
Set AllCells = Range("Database!H3:H5")

' The next statement ignores the error caused
' by attempting to add a duplicate key to the collection.
' The duplicate is not added - which is just what we want!

On Error Resume Next

For Each Cell In AllCells
Brands_Unique.Add Cell.Value, CStr(Cell.Value)
Next Cell

' Resume normal error handling
On Error GoTo 0

' Sort the collection (optional)
For i = 1 To Brands_Unique.Count - 1
For j = i + 1 To Brands_Unique.Count
If Brands_Unique(i) Brands_Unique(j) Then
Swap1 = Brands_Unique(i)
Swap2 = Brands_Unique(j)
Brands_Unique.Add Swap1, befo=j
Brands_Unique.Add Swap2, befo=i
Brands_Unique.Remove i + 1
Brands_Unique.Remove j + 1
End If
Next j
Next i



End Function


+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+






  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Calling (Function?) from Userform_Initialize to generate list of unique values for Combo box

Alan,

Define your variable in a normal module
the module CAN have the option private module

Public Dim g_uni_items as collection

Public Function GetUnique(AllCells As Range, Cell As Range) as
Collection
Dim Brands_Unique As New Collection
Dim i As Integer, j As Integer
'...
set GetUnique = Brands_Unique
end Function

Then in your form or or in somewhere where you need to build the brand
list..

set g_uni_items=GetUniques(Range("a1:a100")

hth


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Alan wrote :


Hi All,

I would like to be able to call a (function?) from the
userform_initialize sub of a userform to populate a combo
with unique items.

I could do that separately in each userform, but since I have quite a
few that need to do it, I thought I would put the code in a separate
sub and just call it each time.

I am using a slightly modified sub I got from j-walk.com
(http://j-walk.com/ss/excel/tips/tip47.htm) - see below bottom
(basically just changed the variable names).

However, I am not sure how to call this from each userform_initialize
sub, return the list of unique items and use that list to populate the
combo.

Part of my problem is how (where?) to declare the variables so that
they exist in each place I need them and how to pass the unique list
back from the sub / function to the initialize code.

Hope that makes sense - feel free to ask for clarification!

Thanks,

Alan.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 188
Default Calling (Function?) from Userform_Initialize to generate list of unique values for Combo box

"keepITcool" wrote in message
...

Alan,

Define your variable in a normal module
the module CAN have the option private module

Public Dim g_uni_items as collection

Public Function GetUnique(AllCells As Range, Cell As Range) as
Collection
Dim Brands_Unique As New Collection
Dim i As Integer, j As Integer
'...
set GetUnique = Brands_Unique
end Function

Then in your form or or in somewhere where you need to build the
brand list..

set g_uni_items=GetUniques(Range("a1:a100")

hth


--
keepITcool
www.XLsupport.com | keepITcool chello nl | amsterdam



Thanks for that - I will have a play!

Alan.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate alphanumeric unique 4 digit values from 12 digit values mikep Excel Worksheet Functions 5 February 9th 07 08:59 PM
Need to generate random values from a list Sumeet Benawra Excel Discussion (Misc queries) 2 July 13th 06 12:13 PM
How do I generate a list from a range of values robo7084 Excel Worksheet Functions 2 July 6th 06 01:48 AM
Populate combo box with unique values only sjayar Excel Discussion (Misc queries) 1 November 7th 05 07:29 AM
Configure Combo Box to Show Unique List Values Only? [email protected] Excel Programming 2 February 5th 04 02:26 PM


All times are GMT +1. The time now is 06:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"