Reusing VBA Codes using Arguments

animas

Active Member
Joined
Sep 28, 2009
Messages
396
I am trying to use the below code to create unique and sorted list dropdown inside a combobox list. What I am looking for is a subruoutine with arguments (List Range, Combobox), so that I can reuse the code for multiple comboboxes. How to modify the below code for this. Codes between "Private Sub UserForm_Initialize() .. End Sub" will be reused.

Thanks.

Code:
Option Explicit
Dim FArray()
Dim DataList As Range
Dim MyList As String
 
Private Sub UserForm_Initialize()
    Dim Found As Long, i As Long
    Dim cel As Range
 
     'Set Range Name to suit
    MyList = "[B]Table_Customer[Customer Name]"[/B]
 
    Set DataList = Range(MyList)
    ReDim FArray(DataList.Cells.Count)
    i = -1
 
    For Each cel In DataList
        On Error Resume Next
        Found = Application.WorksheetFunction.Match(CStr(cel), FArray, 0)
        If Found > 0 Then GoTo Exists
        i = i + 1
        FArray(i) = cel
Exists:
        Found = 0
    Next
    ReDim Preserve FArray(i)
    Call BubbleSort(FArray)
    [B]cmbName[/B].ListRows = i + 1
    [B]cmbName[/B].List() = FArray
End Sub
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Something like:
Code:
Private Sub SortList(DataList As Range, cmbName As MSForms.ComboBox)
    Dim Found As Long, i As Long
    Dim cel As Range
 
    ReDim FArray(DataList.Cells.Count)
    i = -1
 
    For Each cel In DataList
        On Error Resume Next
        Found = Application.WorksheetFunction.Match(CStr(cel), FArray, 0)
        If Found > 0 Then GoTo Exists
        i = i + 1
        FArray(i) = cel
Exists:
        Found = 0
    Next
    ReDim Preserve FArray(i)
    Call BubbleSort(FArray)
    cmbName.ListRows = i + 1
    cmbName.List() = FArray
End Sub
 
Upvote 0
Code:
Private Sub UserForm_Initialize()
    SortList(Table_Customer[Customer Name], cmbCustomerName)...
End Sub

is showing error. My range is a column of a table. how can I pass this as argument?
 
Upvote 0
Remove the parentheses - they don't belong there - and add the Range call:
Code:
Private Sub UserForm_Initialize()
    SortList Range("Table_Customer[Customer Name]"), cmbCustomerName 
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,220
Members
448,876
Latest member
Solitario

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