+ Reply to Thread
Results 1 to 4 of 4

Moving controls in a userform

Hybrid View

  1. #1
    Registered User
    Join Date
    01-19-2006
    Posts
    35

    Moving controls in a userform

    Hi All,
    I have a userform with about 30 various controls on it. I have grouped about 10 of them together so that while I am in the VBE I can just grab that group of controls and move them where I so choose.
    What I would like to do programattically, is to move this group of controls from a point on the userform to another place on the userform at runtime.
    Previously I have been doing this one control at a time, but what I am asking is if anyone knows if there is a way of moving a group of controls together?
    I thought of placing them in an array and then try to move them that way, but there may be an easier/better way that someone knows??
    Any ideas??

    Phil S

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Here's one way: set up three matching arrays:
    myGroup contains the userform controls in your group as objects.
    leftOffset and
    topOffset hold the offsets from the first controls.
    By passing the new location of the control myGroup(0) to the sub Move, all the controls in myGroup will move as a block.
    This could also be done with a 3Xn array.
    Dim myGroup As Variant
    Dim leftOffset() As Long
    Dim topOffset() As Long
    
    Sub setUp()
    Dim i As Integer, newTop As Integer, newLeft As Integer
    
    With UserForm1
        myGroup = Array(.TextBox1, .TextBox1, .ListBox1, .ListBox2)
    End With
    
    For i = 0 To UBound(myGroup)
        leftOffset(i) = myGroup(i).Left - myGroup(0).Left
        topOffset(i) = myGroup(i).Top - mygoup(0).Top
    Next i
    End Sub
    
    Sub moveGroup(newLeft As Integer, newTop As Integer)
    Dim i As Integer
    For i = 0 To UBound(myGroup)
        myGroup(i).Left = newLeft + leftOffset(i)
        myGroup(i).Top = newTop + topOffset(i)
    Next i
    End Sub
    Last edited by mikerickson; 06-22-2007 at 02:55 AM.

  3. #3
    Registered User
    Join Date
    01-19-2006
    Posts
    35
    Ah, Thanks Mike, I will try this out very shortly but it looks very promising..... Ive never used the move class? before.
    Appreciate your help.

    Phil S

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Move is the name I gave to a sub. It's not a class.
    After inspecting the help system, I've edited the sub's name to moveGroup. Move is a no good name for a routine. There is a method Move that applies to a lot of objects, including userforms and controls.
    Last edited by mikerickson; 06-22-2007 at 02:59 AM.

+ Reply to Thread

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