UserForm Controls Position

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,571
Office Version
  1. 365
Platform
  1. Windows
I have a UF with many controls and I want all the controls in the UF to have the .Top value to reduce by the same value.

Rather than having to go through each one and change manually, is there a way to perform this via a routine and for the new .Top values to retained on the UF?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
First, make sure that you allow access to the Visual Basic Project . . .

VBA Code:
Ribbon >> Developer tab >> Code group >> Macro Security >> select/check Trust access to the VBA project model

Then try the following macro . . .

VBA Code:
Option Explicit

Sub test()

    On Error Resume Next
    Dim vbp As Object
    Set vbp = ActiveWorkbook.VBProject
    If vbp Is Nothing Then
        MsgBox "Allow access to the Visual Basic Project, and try again!", vbExclamation, "Programmatic Access"
        Exit Sub
    End If
    On Error GoTo 0
  
    Dim uf As String
    uf = "UserForm1" 'change the userform name accordingly

    On Error Resume Next
    Dim vbc As Object
    Set vbc = vbp.VBComponents(uf)
    If vbc Is Nothing Then
        MsgBox "'" & uf & "' is not available!", vbExclamation, "UserForm"
        Exit Sub
    End If
    On Error GoTo 0
  
    Dim ctrl As Object
    For Each ctrl In vbc.designer.Controls
        With ctrl
            .Top = .Top - .Top
        End With
    Next ctrl
  
    MsgBox "Completed!", vbExclamation, "Completed"
  
End Sub

Note that if you actually meant that you want to reduce the top value by the height, replace . . .

VBA Code:
.Top = .Top - .Top

with

VBA Code:
.Top = .Top - .Height

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,206,969
Messages
6,075,918
Members
446,169
Latest member
luckyfind4u

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top