+ Reply to Thread
Results 1 to 13 of 13

combobox and 2nd value needed

  1. #1
    Forum Contributor
    Join Date
    07-08-2015
    Location
    Indiana
    MS-Off Ver
    Excel 2007
    Posts
    128

    combobox and 2nd value needed

    Hi all

    I have a combobox on my userform that has 2 columns. The combobox has the name cbPartno.

    The first column is the part number and the second column is the part description. They are read in from a data base. All is go there.

    My question is:

    I can get the part number (from column 1) into a variable for use. But how do I get the text description out of the second column and put it in a variable?

    I have tried several codes that give me errors.


    With Sheets("Sheet1")
    .Cells(foundTotals.Row - 2, 1).Value = Me.cbPartno(1).Value


    set description = Me.cbPartno(1).value
    description = Me.cbPartno(1).value


    Thanks

  2. #2
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: combobox and 2nd value needed

    Hello
    Maybe something like:

    Please Login or Register  to view this content.
    *Note column index starts at 0.

    DBY

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: combobox and 2nd value needed

    Two ways to do this.
    1) read from the list
    Please Login or Register  to view this content.
    2) use the TextColumn and BoundColumn properties. Which column the .Text value of the Combobox comes from is determined by the .TextColumn property and the column for the .Value property is determined by the .BoundColumn. NOTE: What you see in the combobox when there is no dropdown is the .Text value of the combobox, not the .Value.

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    07-08-2015
    Location
    Indiana
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: combobox and 2nd value needed

    OK Thanks.

    So here is what I have:

    combo box first column (column(0)) is part number second column (column(1)) is description

    I wish to take the description ( the portion from the second column of the combobox) and put it into a variable for use throughout my program.

    When i use your .textcolumn and boundcolumn properties I get run time errors.

    So, how would I go about taking the description and putting it into a variable for use?

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: combobox and 2nd value needed

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Forum Contributor
    Join Date
    07-08-2015
    Location
    Indiana
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: combobox and 2nd value needed

    Using the code you gave me here are the errors I get:

    If I put in the .textcolumn and boundcolum in the initalize procedure,

    I get an error in the "click" procedure where I am displaying both colums. Code:
    Please Login or Register  to view this content.
    If I take out the .text and .bound, it work ok, but then I get the same error 381 could not get the list property invalid property array index

    Please Login or Register  to view this content.
    I have tried in in the "click" initalize and command button procedures.

    Maybe this will help a bit. Here is the initalize procedure and how the colums are being read in:

    Please Login or Register  to view this content.
    Sorry for teh novie questions and issues. Still learning the VBA programming.

    Thanks so much

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: combobox and 2nd value needed

    Made you simple example to show how it works.
    Select value in combobox and then msgbox displays value from second column.
    Attached Files Attached Files

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: combobox and 2nd value needed

    Please Login or Register  to view this content.
    This looks like you are trying to set the .Value of the combox to a value that isn't in the combobox.

    Please Login or Register  to view this content.
    This will error if the .ListIndex = -1, i.e. if there is nothing selected from the combobox.

    Please Login or Register  to view this content.
    This can be replaced with the non-looping
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    07-08-2015
    Location
    Indiana
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: combobox and 2nd value needed

    Quote Originally Posted by bakerman2 View Post
    Made you simple example to show how it works.
    Select value in combobox and then msgbox displays value from second column.
    Your code work great in your example. Unfortunately, in my code, it fails at the line
    Partno = .list(.ListIndex, 0) ' where I am attempting to take the part number (whats in the first column) and put it invariable Partno for use later in program.

    I keep getting Runtime error 381. Could not get the List property Invalid property array index.

    I'm attaching the file with code so you can see. I don't think anything would be changing and causing it to fail.

    It may be that I am not familiar enough with VBA coding and the code for .ListIndex, 0 is the issue.


    Thanks
    Attached Files Attached Files

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: combobox and 2nd value needed

    I notice a couple of problems with the attached file.
    First off, there is a Workbook_Open event in the user form HeaderInfo code module, which is never called.

    More to the point of this thread, in the other user form
    Please Login or Register  to view this content.
    The variables Partno and description are scoped only to that procedure, which is why their value is vbNullString when referred to outside of that procedure. They need to be scoped to be module wide variables, by putting their declaration lines before any procedure and after the (missing) Option
    Explicit line.

    Please Login or Register  to view this content.

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: combobox and 2nd value needed

    Made a few changes to show how it works.
    Open userform, select partNo and click Messagebox button.
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    07-08-2015
    Location
    Indiana
    MS-Off Ver
    Excel 2007
    Posts
    128

    Re: combobox and 2nd value needed

    OK. It looks good and I understand how it works.

    I noticed that you commented out the part about loading the data for teh drop down menu from the data base on our network drive.
    I tried to adapt your code and add those back in, but I keep getting either run time or complie errors (subscript...)

    So how would I incorporate your code for the getting of the data from the drop down menu and use of the data base on the network drive
    to fill it? Use of the network drive is important as it is where all the parts get added when we add or revise parts.

    thanks

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: combobox and 2nd value needed

    I just commented that out to get it working for me.
    If your parts database really starts at row 883 then this works just fine.
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Multiple Combobox User Form HELP needed.
    By sirJohnyQ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-12-2014, 01:35 PM
  2. [SOLVED] combobox within a frame is different code needed
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-01-2014, 02:50 PM
  3. VBA / Macro help needed for pupulate records in forms based on two combobox seletion
    By krjoshi in forum Access Programming / VBA / Macros
    Replies: 8
    Last Post: 10-09-2013, 08:32 AM
  4. [SOLVED] UserForm and Combobox help needed
    By Surreall in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-25-2013, 05:49 AM
  5. [SOLVED] Help needed to link combobox and textboxes
    By krjoshi in forum Access Programming / VBA / Macros
    Replies: 12
    Last Post: 01-04-2013, 06:27 PM
  6. Userform Multipage Driven by Combobox Solution Needed
    By devine_tom in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-24-2012, 02:19 AM
  7. ComboBox / Listbox help needed
    By MichaelBenn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-12-2009, 11:24 AM

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