Forum Discussion
DanMcG
Jan 22, 2021Brass Contributor
Can I programmatically change properties of selected userform controls when I am designing the form?
I have created code to add 4 new commands to a context menu (MSForms Control) that is shown when I right click on selected controls in a userform. Basically I want to nudge the selected controls in a...
ExcelHCFUser
Feb 15, 2021Copper Contributor
ThisWorkbook.VBProject.VBComponents("UserForm1").designer.controls("ComboBox1").left = 0
and/or
ThisWorkbook.VBProject.VBComponents("UserForm1").designer.controls("ComboBox1").top = 0
note that ThisWorkbook.VBProject.VBComponents("UserForm1").designer properties and methods are hidden and there is no intellisense available
DanMcG
Feb 18, 2021Brass Contributor
This can be done by accessing the .Designer property of the selected vbcomponent. Below will move the selected controls a specific direction a number of pixels.
Sub nudgeHor(distanceInPixels As Long)
Dim ctl As Control
For Each ctl In ThisWorkbook.VBProject.VBComponents(Application.VBE.SelectedVBComponent.Name).Designer.Selected
ctl.Left = ctl.Left + distanceInPixels
Next ctl
End Sub
Sub nudgeVer(distanceInPixels As Long)
Dim ctl As Control
For Each ctl In ThisWorkbook.VBProject.VBComponents(Application.VBE.SelectedVBComponent.Name).Designer.Selected
ctl.Top = ctl.Top + distanceInPixels
Next ctl
End Sub
- BartH_NLApr 12, 2025Copper Contributor
This is correct. You can even use the .designer to activate controls in the VBIDE if you don't know where they are.