+ Reply to Thread
Results 1 to 2 of 2

Problem setting combobox

  1. #1
    Post Tenebras Lux
    Guest

    Problem setting combobox

    I have a userform with multiple comboboxes (combobox1 -> combobox40)

    I want to fill the combobox, preferably using .additem , so that the user
    can't change it. Somehow, I can't set the cboBox variable.

    This is what I've tried:

    Sub FillAllComboBoxes()
    dim cboBox as msforms.combobox
    dim frmSettings as userform

    frmSettings = SettingsForm ' the name of my userform

    With frmSettings
    .combobox1.additem = "FirstItem" 'works just fine
    set cboBox = .combobox1 ' bugs out here
    call FillcomboBox(cboBox)
    End with
    end sub


    Sub FillcomboBox(cboBox as MsForms.combobox)

    cboBox.additem = "value1"
    cboBox.additem = "value2"

    End sub

    I can't get passed the
    set cboBox = .combobox1
    line without it bugging out.

    Ideally, I'd like to cycle through the combobox using their numbers. I saw
    this posting by Tom Ogilvy, but can't get it to work for a userform that is
    not part of an active worksheet.

    ' this declaration is important
    Dim cbx as MsForms.Combobox
    .. . .
    For i = 400 To myrow - 10 Step -1
    With ActiveSheet.OLEObjects("ComboBox" & i)
    .name = "ComboBox" & i + 1
    set cbx = .Object
    cbx.Name = "ComboBox" & i + 1
    end with
    Next i

    --







  2. #2
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    Hi try this modified code, it worked for me for Combobox1 i'm using Excel 2003 Windows XP.

    Regards,
    Simon

    Sub FillAllComboBoxes()
    Dim cboBox As MsForms.ComboBox
    With settingsform
    .ComboBox1.AddItem "FirstItem"
    Set cboBox = .ComboBox1
    Call FillcomboBox(cboBox)
    End With
    End Sub


    Sub FillcomboBox(cboBox As MsForms.ComboBox)

    cboBox.AddItem "value1"
    cboBox.AddItem "value2"

    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1