Excel VBA modify code for ComboBox data loading

Aretradeser

Board Regular
Joined
Jan 16, 2013
Messages
176
Office Version
  1. 2013
Platform
  1. Windows
I want to use a ComboBox in a Userform to load data, the problem is that in the column where this data is located there are empty cells and the code I use doesn't work.
This is the VBA code I use (If there are no empty cells in the column it works):
What modification should I make in this code so that the ComboBox5 loads the records, as unique and ordered values, avoiding the empty cells?

VBA Code:
Dim dar As Object
Dim va
Dim n As Integer

Private Sub UserForm_Initialize()
    With Sheets("BDATOS")
        n = .Range("B" & Rows.Count).End(xlUp).Row
        va = .Range("C2:C" & n)
    End With
    Set dar = CreateObject("System.Collections.Arraylist")
End Sub

'
Private Sub ComboBox5_Enter()
    Dim x
    dar.Clear
    For Each x In va
        If Not dar.Contains(x) Then dar.Add CStr(x)
    Next
    dar.Sort
    ComboBox5.List = dar.toArray()
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about
VBA Code:
If x <> "" And Not dar.Contains(x) Then dar.Add CStr(x)
 
Upvote 0
Solution
Shall I replace this line of code with yours?
VBA Code:
If Not dar.Contains(x) Then dar.Add CStr(x)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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