+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    12-08-2009
    Location
    Madison, WI
    MS-Off Ver
    Excel 2003
    Posts
    30

    Looping through Frames

    Hopefully this will be a quick fill in the blank.

    I'm wanting to loop through the first 13 Frames of a Userform and think the below code will do what I want, but I have been unable to figure out how to set cFrame.

    Code:
    Dim Ctrl as Control
    Dim cFrame as Frame
    
    X = 1 to 13
        cFrame = What goes here?
        For Each Ctrl in cFrame.Controls
            Doing stuff here.
        Next Ctrl
    Next X
    Last edited by magness; 01-27-2010 at 01:40 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Looping through Frames

    something like this,

    Code:
        Dim cltTemp As MSForms.Control
        Dim lngCount As Long
        
        For Each cltTemp In Me.Controls
            If TypeOf cltTemp Is MSForms.Frame Then
                lngCount = lngCount + 1
                If lngCount > 13 Then Exit For
                Debug.Print cltTemp.Name, cltTemp.Caption, cltTemp.Controls.Count
            End If
        Next
    Order appears to be based on that of controls creation
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    12-08-2009
    Location
    Madison, WI
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Looping through Frames

    hmm... Less than ideal since I'd have to rearrange all the frames (they weren't created in any specific order) They are however named sequentially. Is there a way to say something like cFrame = "Frame" & X?

    Thank you though. If there isn't a way to do the above this will be very helpful.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Looping through Frames

    If the controls all have the same name then

    Code:
        Dim cFrame As MSForms.Frame
        Dim lngIndex As Long
        
        For lngIndex = 1 To 13
            Set cFrame = Me.Controls("Frame" & lngIndex)
            Debug.Print cFrame.Name, cFrame.Caption, cFrame.Controls.Count
        Next
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    12-08-2009
    Location
    Madison, WI
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Looping through Frames

    Thank you, this clears it up. Sorry I wasn't more specific in the initial post and mentioning they were named sequentially.

    I had tried:

    Code:
        Dim cFrame As MSForms.Frame
        
        For X = 1 To 2
            cFrame = Me.Controls("Frame" & X)
            For Each Control In cFrame.Controls
                Debug.Print Control.Name
            Next Control
        Next X
    And it didn't work. But yours does. The only real difference I can see is using Set for cFrame. What is the difference between saying "cFrame =" and "Set cFrame ="?

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Looping through Frames

    You need to use the Set keyword in order to assign a reference to an object.
    Cheers
    Andy
    www.andypope.info

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.2.0