+ Reply to Thread
Results 1 to 9 of 9

Thread: how to get value of a combobox list in a worksheet from code in a module

  1. #1
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Arrow how to get value of a combobox list in a worksheet from code in a module

    hi there,

    i have a combobox in a worksheet, and the associated list has 3 columns

    I need the value of the third column (col index = 2) in a code I have in one of the modules.

    following this thread (http://www.pcreview.co.uk/forums/thread-989754.php) I have tried the myworksheet.oleobjects("mycombobox").object.list(.... ) but it does not seem to work

    any hints on how to do that? (which should be quite easy I guess... )

    thanks!

    Bernat
    Last edited by bagullo; 10-28-2010 at 11:07 AM.

  2. #2
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: how to get value of a combobox list in a worksheet from code in a module

    Hi,

    If I understand you correctly you are wanting to see the 3rd item in the combobox when the form is opened.

    Make sure that you have the ColumnCount property set to 3, then in the ColumnWidth property enter

    0,0,20

    if the last 20 is not wide enough just change it. You may also need to use semi colons instead of commas depending on your local settings.

    HTH
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  3. #3
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: how to get value of a combobox list in a worksheet from code in a module

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    If I understand you correctly you are wanting to see the 3rd item in the combobox when the form is opened.

    HTH
    well actually no. Apologies for sloppy explanation.
    I'm happy the way the combobox displays, but I want to access by code to the values present on the third column which are not visible to the user since the column has a width of 0 pixels.

    any suitable way to access the .list method of a combobox in a worksheet (the code is on a standard module)

    thnx!

  4. #4
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: how to get value of a combobox list in a worksheet from code in a module

    Hi,

    OK, how about

    MsgBox UserForm1.ComboBox1.Column(2)

    where the (2) is the third column since the index is zero based.

    HTH
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: how to get value of a combobox list in a worksheet from code in a module

    Hello ,

    Since you did not say which toolbar control you are using, I am providing examples for both types. These examples assume that the code is in a standard VBA module, like Module1. These are only basic examples. There is no object validation or error handling in these macros..

    Apparently you have used the ListFillRange property to fill these controls. The ListFillRange property property is available to the Control Toolbox ComboBox control only when it is on a worksheet. The properties for Control Toolbox controls may change depending on where they are placed: VBA UserForm or Worksheet.

    Control Toolbox ComboBox Code
    This will return the Column 3 value to the variable "X".
    Sub GetComboBoxColumn3()
    
     'For Control Toolbox ComboBox Control
     
      Dim CB As OLEObject
      Dim Wks As Worksheet
      Dim X As Variant
      
        Set Wks = Worksheets("Sheet1")
        Set CB = Wks.OLEObjects("ComboBox1")
        
        X = CB.Object.List(CB.ListIndex, 2)
      
    End Sub

    Forms Toolbar DropDown Code
    This will return the Column 3 value to the variable "X".
    Sub GetDropDownColumn3()
    
     'For the Forms Drop Down Control
     'Assign this macro to the Drop Down
     
      Dim DD As Excel.DropDown
      Dim Rng As Range
      Dim Wks As Worksheet
      Dim X As Variant
      
        Set Wks = Worksheets("Sheet1")
        Set DD = Wks.DropDowns(Application.Caller)
        
        Set Rng = Evaluate(DD.ListFillRange)
        
        X = Rng.Cells(DD.ListIndex, 3)
        
    End Sub

    If you have any questions, please ask.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: how to get value of a combobox list in a worksheet from code in a module

    thanks a lot!
    i was attempting to do the same using a with, but that did not work well... using a oleobject variable makes it work and readable at the same time

    btw, in case somebody attempts the first solution... there was a .object missing in the code
    X = CB.Object.List(CB.ListIndex, 2) --> CB.object.list(cb.object.listindex,2)

    I am using control buttons! sorry for forgetting to say that

    regards !

  7. #7
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: how to get value of a combobox list in a worksheet from code in a module

    btw 2 -- could not figure out how to mark this thread as [SOLVED], but it certainly is

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: how to get value of a combobox list in a worksheet from code in a module

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  9. #9
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: how to get value of a combobox list in a worksheet from code in a module

    piece of cake!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0