Results 1 to 3 of 3

Thread: Setting Combox values using "For Each" statement ...

  1. #1

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Resolved Setting Combox values using "For Each" statement ...

    I've got a form in Excel that contains 20+ comboboxes.
    Unfortunately I can't use control arrays, but rather than setting the rowsource for each combobox in turn I am trying to do the following :

    VB Code:
    1. Dim cbo As ComboBox
    2.    
    3.     For Each cbo In [B]forms[/B]
    4.         cbo.RowSource = "'Data Source'!P3:P4"
    5.     Next

    For some reason this doesn't work ?!?!
    The "forms" object seems empty ?!!
    Do I have to address it as something else in VBA ?
    Last edited by TheBionicOrange; Mar 22nd, 2006 at 11:43 AM.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Setting Combox values using "For Each" statement ...

    BO
    Are you trying to set the rowsource in the UserForm_Initialize event? If so, then you need to use the Controls collection of the Form to loop through the comboboxes.
    Here's some sample code. Note: I am assuming that each combobox has a name that begins with "cbo".
    VB Code:
    1. Private Sub UserForm_Initialize()
    2. Dim MyCombo As Control
    3.    
    4.     For Each MyCombo In Me.Controls
    5.         If Left(MyCombo.Name, 3) = "cbo" Then
    6.             MyCombo.RowSource = "'Data Source'!P3:P4"
    7.         End If
    8.     Next
    9. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Setting Combox values using "For Each" statement ...

    Perfect !

    Thank You !!

Posting Permissions

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



Click Here to Expand Forum to Full Width