Consulting

Results 1 to 5 of 5

Thread: Excel ComboBox - Setting Value

  1. #1
    VBAX Regular
    Joined
    Aug 2007
    Posts
    6
    Location

    Excel ComboBox - Setting Value

    I'm having trouble setting the display value of a combobox on a worksheet from code. This is not the ActiveX ComboBox, but the one built into the Excel Forms objects. Here's what I have:

    Dim CB as Object

    Set CB = WS.DropDowns.Add(Left, Top, 128.25, 15.75)
    With CB
    .Name = Name
    .ListFillRange = Source
    .DropDownLines = 8
    .Display3DShading = False
    .OnAction = Name & "_Change"
    End With
    CB.ListIndex = 0
    I thought setting the ListIndex would force the text, but it does not.

    I found a reference telling me the following format to retrieve the text:
    SelectedValue = WS.DropDowns("cmbServices").List(WS.DropDowns("cmbServices").ListIndex)

    This works from the Combobox's Change event, but it doesn't seem to work when I just add the combobox on the fly using the above code. It balks at the .List property saying it can't be found.
    Any suggestions?
    Thx.
    Craig

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Are you sure that you are actually loading the DD? Listfillrange should be a string cell address, not an array of values. If you want the array, use List.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    The ListIndex starts at 1 so if you set it to 0 you will get a blank. It will also fail if you try and access List(0) because List(1) is the first item.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    VBAX Regular
    Joined
    Aug 2007
    Posts
    6
    Location

    Thanks Again!

    Setting the ListIndex to 1 instead of zero was my entire problem. I thought the .List was an array, so it would be zero based. I should have asked you guys first, it would have saved me about 2 days looking into why .List was not available from the VB6 code I'm using to create this and yet it worked fine inside Excel (once I had selected a value in the combobox, so the combobox's .ListIndex was no longer set to zero when VB6 tried to use it)!!!!
    Some of the biggest pains are solved with the simplest solutions.
    Thx.
    Craig

  5. #5
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    It would be more useful if it gave a Subscript Out of Range error, rather than a failure to get the List property, wouldn't it!
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

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