Consulting

Results 1 to 12 of 12

Thread: Solved: Combo Box: set possible values 1 thru N in code

  1. #1
    VBAX Regular
    Joined
    Jan 2007
    Location
    Dallas area
    Posts
    74
    Location

    Solved: Combo Box: set possible values 1 thru N in code

    Hello all!

    I have a userform that can select from a large number (several hundred) of items, and depending on other factors, the number of items on that list can change. Each item is simply a whole number 1 thru (max item #). I'd like to use a combo box that the user can use to select any valid item #; is there a way to tell the combo box that it should offer values of 1 thru N?

    I considered the add item method, but it seems inefficient to do a for-next loop to add a couple hundred items to the combo box, then clear it when the list of items changes (can change up or down). I guess I could just put a range of numbers someplace in a spreadsheet & tell the combo box the range, but that also seems inefficient to have a few hundred cells devoted to nothing but numbers 1 thru a couple hundred.

    So... is there an efficient method I'm missing, or should I just do the for-next loop & add items each time?

    Many thanks,

    G.T.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is loading an array and pointing that at the combob any better in your view?
    ____________________________________________
    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
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You could add the numbers to the sheet then clear them
    [vba]Private Sub UserForm_Initialize()
    Dim Rng As Range
    Set Rng = Range("Z1:Z100")
    Rng = "=Row()"
    Me.ComboBox1.List() = Rng.Value
    Rng.ClearContents
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Jan 2007
    Location
    Dallas area
    Posts
    74
    Location
    Thanks, Malcolm. I'll go that route, setting the range to however far down the list of numbers I need & pointing the combo box at that range. As often as I'll be resetting the range, I will probably just devote a column to numbers, and not bother clearing & re-setting them every time. Cells are cheap; why spend the cycles?

    Is loading an array and pointing that at the combob any better in your view?
    I'm still new enough at this that I'm not sure my view has much credential; doing a for-next loop to add items seems (and I'm perfectly willing to be corrected here) like a lot of cycles to go thru; setting the size of the range looks like the fewest lines of code, which is an imperfect proxy for "efficient solution." As little as I know about VBA, I'd think I'd need a loop of some sort to set the values 1 thru 344 (or whatever) in the 344 elements of an array, so based on a limited knowledge, I'd say that the array doesn't seem to be an efficient way to go either. But again, it wouldn't surprise me to find that there's a far more elegant way to go about it that I'm not considering.

    I've had experience (and actual training) in Basic, Pascal, and Ada, although nothing more recent than about 1989. Picking up VBA goes fairly quickly in some things, not at all so fast in others, and I regularly find an elegant way of doing something that simply would not have occurred to me. In a worksheet, setting .value = .value instead of doing copy/paste special / values is one example; there are plenty more.

    Anyway, I wondered if there was a means I couldn't see to tell the combo box to display values 1 thru N. Malcolm's answer is good, and that being a quick & easy solution, tells me all I need to know. (i.e. no, there is no property to set .abc to true and then setting .xyz = 412 gives you the 1-412 range you're looking for).

    If the array approach would be better, I'd be interested to see it; is that the way you'd set the list of the combo box, Bob?

    Thanks,

    G.T.

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    or maybe the RowSource property...
    If you want the entire column,
    [vba] With Sheets("DATA")
    .Activate
    ComboBox1.RowSource = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Address
    End With[/vba]
    If you only want part of the column
    [vba]With Sheets("DATA")
    .Activate
    ComboBox1.RowSource = .Range("A2:A12").Address
    End With[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I don't believe it will make a difference with a few hundered rows, but saving 1 to 65536 in as column increases the workbook size from 30kb to 3.5mb. There seems no noticable time difference (0.26 sec v 0.13 sec) in writing/deleting the values as against reading them from a saved column.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GreenTree
    As little as I know about VBA, I'd think I'd need a loop of some sort to set the values 1 thru 344 (or whatever) in the 344 elements of an array, so based on a limited knowledge, I'd say that the array doesn't seem to be an efficient way to go either.
    344 items in such a loop is almost irrelevant, it is tiny.
    ____________________________________________
    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

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Looping 344 items timed at 0.016 sec.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Okay, so I'm wrong! Didn't realise it would take that long.
    ____________________________________________
    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

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Maybe the timer is causing the extended delay. Now if only we could time without the timer!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    ____________________________________________
    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

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Bob,
    I'll have a look at this.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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