Consulting

Results 1 to 6 of 6

Thread: Leave out blank items in combobox

  1. #1

    Leave out blank items in combobox

    Hi!!
    Does anybody know if there is a procedure that will help me leave out blank items in a combobox.

    The items in the combobox comes from a range in another sheet where there are some blanks.

    thnx alot!

    /Jack

  2. #2
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    combobox from the forms toolbar on the sheet?
    combobox from the control toolbox on the sheet?
    combobox on a userform?
    data validation drop down on a sheet?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    i get blank cells in a combobox in a userform.
    those are the ones that i would like to get rid of.

  4. #4
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Jack,
    This will return a list of unique items from the column with no blanks.
    [vba]Option Explicit
    Private Sub UserForm_Initialize()
    Dim UniqueList() As String
    Dim x As Long
    Dim Rng1 As Range
    Dim c As Range
    Dim Unique As Boolean
    Dim y As Long

    Set Rng1 = Sheets("Sheet1").Range("A3:A1103")
    y = 1

    ReDim UniqueList(1 To Rng1.Rows.Count)

    For Each c In Rng1
    If Not c.Value = vbNullString Then
    Unique = True
    For x = 1 To y
    If UniqueList(x) = c.Text Then
    Unique = False
    End If
    Next
    If Unique Then
    y = y + 1
    Me.ComboBox1.AddItem (c.Text)
    UniqueList(y) = c.Text
    End If
    End If
    Next

    End Sub[/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Perfect - works fine.
    Just what I needed!!

  6. #6
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    And if you don't want a unique list of items from the column but you want no blank spaces just change it like this:
    [vba]Option Explicit
    Private Sub UserForm_Initialize()
    Dim Rng1 As Range
    Dim c As Range
    Set Rng1 = Sheets("Sheet1").Range("A3:A1103")
    For Each c In Rng1
    If Not c.Value = vbNullString Then
    Me.ComboBox1.AddItem (c.Text)
    End If
    Next
    End Sub
    [/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •