+ Reply to Thread
Results 1 to 4 of 4

Displaying label from a userform combobox selection

  1. #1
    Registered User
    Join Date
    11-22-2012
    Location
    Lincoln, England
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    2

    Question Displaying label from a userform combobox selection

    Hi,

    I am just starting out with VBA and would like to know how to write a code that will display information in a Label (label4) depending on what is selected from a combo box (combobox1) - I have defined the combobox range using its properties settings.

    For example the combo box contains text in range A1:A40. If A10 is selected in the combobox I would like to populate label4 with the text contained in the column adjacent, B10.

    I Think I need to use the offset command but is im new to VBA and programming in general I need a helping hand with writing the code.

    Any help is very gratefully recieved.

    Many Thanks,
    Pete.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    11-22-2012
    Location
    Lincoln, England
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    2

    Re: Displaying label from a userform combobox selection

    Thanks for that - I made the following ammendments to get it to work for me (and changed the label to a textbox).

    Private Sub Combobox1_Change()
    If Combobox1.ListIndex <> -1 Then
    Textbox1 = Range("B" & Combobox1.ListIndex+2).Value
    End If
    End Sub

    I needed the contents from row B, not sure why I had to use +2 for the listindex but this does work - can you explain the logic? (now displays the text in the column one to the right of the combobox selection.

    Also the wordwrap wont work for the text box. I have added
    private Sub TextBox1_Change()
    TextBox1.WordWrap = True
    End Sub

    I have also made sure that word wrap is enabled in the spreadsheet and textbox properties - any ideas?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Displaying label from a userform combobox selection

    The ListIndex of a combobox(or listbox) is the tells you which item has been selected.

    It's starts at 0, so if the 1st item is selected the ListIndex is 0, if the 2nd item is selected it's 1, the 3rd its 2 and so on.

    So to get the row number of the selected item we need to add to the ListIndex.

    What to add depends on how you've populated your combobox, in particular which row you've started from.

    For example if you've populated using A1:A10 then you need to add 1 to the ListIndex to get the row, if it's was A2:A11 you would add 2 and so on.

    Hope that explains a bit, and sorry about the mistakes.

+ 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