Enhancing The Creation Of A Combobox List

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,562
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to create one single combobox list by combining values from 2 separate ranges in a worksheet.

Code:
       With ws_vh
            lrow_a = .Cells(.Rows.Count, "L").End(xlUp).Row
            lrow_p = .Cells(.Rows.Count, "M").End(xlUp).Row
            For Each Rng In Array(.Range("L2:L" & lrow_a), .Range("M2:M" & lrow_p))
                For Each cll In Rng
                    cb_mri.AddItem cll.Value
                Next cll
            Next Rng
        End With

This code kinda works. Here is my data:

Book1
LM
1ACTIVEPASSIVE
284992
VAR_HOLD


The resulting list that shows in the combobox is

Book1
P
9ACTIVE
10
1184992
VAR_HOLD

This is just a representation of what is shown ... 1st value is active, 2nd value an empty value and the 3rd value is really all I'm looking for, 84992.

What I am looking for help doing:
1) the combobox list should only contain numbers found in those two columns (no balnks, no headers)
2) eliminate any duplicates between the two columns (there are no duplicates in the individual columns)
3) autopopulate the combobox value with the first value in the list.
4) If there is only one value in the list, disable the combobox (locked vs enabled so that it doesn't get greyed out)

Any support will be greatly appreciated.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Something like
Code:
Private Sub UserForm_Initialize()
   Dim Cl As Range
   Dim Ws As Worksheet
   Set Ws = Sheets("Log")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws.Range("L2:L200", "M2:M200")
         If Cl <> "" Then .Item(Cl.Value) = Empty
      Next Cl
      Me.ComboBox1.List = .Keys
      Me.ComboBox1.ListIndex = 0
      If .Count = 1 Then Me.ComboBox1.Locked = True
   End With
End Sub
 
Upvote 0
Something like
Code:
Private Sub UserForm_Initialize()
   Dim Cl As Range
   Dim Ws As Worksheet
   Set Ws = Sheets("Log")
   
   With CreateObject("scripting.dictionary")
      [B][COLOR="#FF0000"]For Each Cl In Ws.Range("L2:L200", "M2:M200")[/COLOR][/B]
         If Cl <> "" Then .Item(Cl.Value) = Empty
      Next Cl
      Me.ComboBox1.List = .Keys
      Me.ComboBox1.ListIndex = 0
      If .Count = 1 Then Me.ComboBox1.Locked = True
   End With
End Sub

It probably won't make a difference time-wise for the OP's data but, assuming the OP's data are constants (not formulas), you make your loop do less looping by changing the highlighted line of code to this...
Code:
For Each Cl In Ws.Columns("L:M").SpecialCells(xlConstants, xlNumbers)
Also, do it this way saves the OP from having to determine where the last row of data is (something the OP did in his posted code but that you did not do in yours).
 
Last edited:
Upvote 0
Thank you both! Your help is so very helpful and I appreciate it.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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