+ Reply to Thread
Results 1 to 15 of 15

Refer to Combobox passed as parameter by string variable name

  1. #1
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Refer to Combobox passed as parameter by string variable name

    Hi All,

    I feel like this should be easy but for the life of me I can't work it out!

    I have a UserForm with various controls on it including some Comboboxes. When a change is made it triggers the change event and calls a sub in another module, passing the relevant controls as parameters:
    N.B. I've simplified it here to just 3 of each control, when there are actually several.

    Please Login or Register  to view this content.
    The UpdateTotal2 Sub then checks if there are different currencies listed in the adjacent comboboxes and if they are all the same totals the textbox values and populates a total:

    Please Login or Register  to view this content.
    The problem occurs with this line of code:
    Please Login or Register  to view this content.
    If I simply use:
    Please Login or Register  to view this content.
    Then the code works fine, but that will require me to reiterate the same line of code for every combobox of which there are many (simplified here). I've tried every variation I can think of, msforms.combox, UserForm2.Controls, .name, Val("cbo" & objNo), etc, etc.

    Is there a way that I can reference the parameter by string variable?

    Thanks, TC

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Refer to Combobox passed as parameter by string variable name

    Hi,

    If that code is also in the userform, you may use
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Refer to Combobox passed as parameter by string variable name

    Hi xlnitwit,

    No, the UpdateTotal2 sub is held in a separate module. If I substitute the "Me." with the userform name I get the error: "Could not find the specified object". Presumably because it is looking for a control named cbo2 rather than Combobox10.

    I suppose I could move the code to the userform to avoid having to pass the parameter, it's just really bugging me as I feel like there should be a way to do this...

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Refer to Combobox passed as parameter by string variable name

    Why would you want to? It's much more logical for the code to be in the userform.

    Would could pass the userform as a parameter of the sub so you could have something like:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Refer to Combobox passed as parameter by string variable name

    Hi Kyle123,

    Because the same piece of code is referenced by more than one userform, so it made sense to me to store it centrally so both could access rather than duplicating in each userform.

    Wouldn't passing the Userform be exacly the same as I tried above, resulting in "Could not find the specified object"? As there is no such control named cbo in the userform, rather it refers to a parameter that contains the combobox name...?
    But I can see how passing just the userform and calling the controls by their 'real' name would work. so thanks, that's given me something to work with.

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Refer to Combobox passed as parameter by string variable name

    Yes, sorry, I misunderstood what you are trying to do.

    Do you have multiple identical userforms?

    Are you calling that same function whenever any of those controls change?

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Refer to Combobox passed as parameter by string variable name

    I fear I misunderstood your intent. You might perhaps pass one array of controls instead of three separate parameters.
    Please Login or Register  to view this content.
    unless I still have not understood.
    Last edited by xlnitwit; 12-20-2016 at 11:01 AM.

  8. #8
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Refer to Combobox passed as parameter by string variable name

    No worries Kyle123.

    They aren't identical userforms, different but require the same function, i.e. totalling textboxes if the currency is the same. And yes, it's called by the textboxes change event where the cash value is input and also the comboboxes change event when the currency is changed. May seem a bit overkill but the only way I could find to get 'live' updating.

  9. #9
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Refer to Combobox passed as parameter by string variable name

    xlnitwit, ah okay.

    So by doing this, presumably it will 'pass' all comboboxes over, rather than the three given in the example? So cbo(1) would mean Combobox1? Rather than Combobox5 as when passed individually?

    Not a problem as I can use a multiplier to get the correct Combobox name, just want to make sure I'm understanding it right?

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Refer to Combobox passed as parameter by string variable name

    You could use a class module and then add the textboxes to the class... then have the class handle their behaviour.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Refer to Combobox passed as parameter by string variable name

    cbo(1) would be the first item in the array that you pass to the routine. If you wish to refer to them by name instead, you might use a Dictionary or Collection rather than an array. For any of these options, you will need to alter the way you are passing the controls currently. If you don't want to do that, you might use a ParamArray instead.

  12. #12
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Refer to Combobox passed as parameter by string variable name

    So forgive my ignorance here xlnitwit, how would I pass select comboboxes as an array?

    Please Login or Register  to view this content.
    I've only worked with basic string arrays really.

    Arkadi, I'd read about creating a class module and I'm guessing there are other more suitable alternatives but I don't really want to cause myself a huge amount of rework if it can be avoided.

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Refer to Combobox passed as parameter by string variable name

    If you wish to use the Array function, you will need to alter the routine declaration thus
    Please Login or Register  to view this content.
    Otherwise you will need to explicitly declare an array of combobox type and populate it with a loop.

  14. #14
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Refer to Combobox passed as parameter by string variable name

    Okay so
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    I will give it a try. Thanks for your help.

  15. #15
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: Refer to Combobox passed as parameter by string variable name

    Works perfectly, thanks all.

    For clarity this is the code I used:
    Please Login or Register  to view this content.
    And
    Please Login or Register  to view this content.
    TC

+ 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. [SOLVED] Getting Type Mismatch with String passed to a Function with String parameter
    By allansy8 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-30-2014, 10:33 PM
  2. Replies: 0
    Last Post: 05-29-2014, 09:38 AM
  3. [SOLVED] VBA: Array of User Defined Type passed as parameter to function
    By Bezzie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-14-2013, 07:08 AM
  4. How to refer variable while assigning string value?
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-07-2006, 03:10 AM
  5. [SOLVED] Set string variable to word displayed in a combobox
    By Graham Whitehead in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2006, 09:30 AM
  6. Refer to combobox in userform with variable
    By lif in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-25-2006, 06:25 AM
  7. [SOLVED] Excel2000: Reading values from range, passed to function as parameter using an expression
    By Arvi Laanemets in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-29-2005, 10:06 AM

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.6.0 RC 1