Table of contents
Collapse the table of content
Expand the table of content

Controls Collection, Move Method Example

office 365 dev account|Last Updated: 6/12/2017
1 Contributor

The following example accesses individual controls from the Controls collection using a For Each...Next loop. When the user presses CommandButton1, the other controls are placed in a column along the left edge of the form using the Move method.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains a CommandButton named CommandButton1 and several other controls.

Dim CtrlHeight As Single 
Dim CtrlTop As Single 
Dim CtrlGap As Single 

Private Sub CommandButton1_Click() 
 Dim MyControl As Control 
 CtrlTop = 5 

 For Each MyControl In Controls 
 If MyControl.Name = "CommandButton1" Then 
 'Don't move or resize this control. 
 'Move method using named arguments 
 MyControl.Move Top:=CtrlTop, _ 
 Height:=CtrlHeight, Left:=5 

 'Move method using unnamed arguments (left, 
 'top, width, height) 
 'MyControl.Move 5, CtrlTop, ,CtrlHeight 

 'Calculate top coordinate for next control 
 CtrlTop = CtrlTop + CtrlHeight + CtrlGap 
 End If 

End Sub
Private Sub UserForm_Initialize() 
 CtrlHeight = 20 
 CtrlGap = 5 

 CommandButton1.Caption = "Click to move controls" 
 CommandButton1.AutoSize = True 
 CommandButton1.Left = 120 
 CommandButton1.Top = CtrlTop 
End Sub
© 2018 Microsoft