Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How to nudge or move selected controls in a userform

0

How can I loop through the selection set in a userform controls? i tried this but it didn't work. i know how to loop through all the controls, but not the selection set.

Sub LoopSelectedControls()
If Selection.count < 1 Then Exit Sub
Dim obj As Control
With Selection
For Each obj In .Controls
    Debug.Print TypeName(obj)
Next obj
End With
End Sub

so i have learned how to put a command "Nudge Up" on the context menu for userform controls "MSForms Control" and now need to cycle through any selected controls on the userform to nudge in a particular direction (Up/Down/Left/Right). in working with the selection command, that is only for selecting cells (at least that is what i found so far). so is it possible to create a selection set of selected controls on a userform?

Post Edited
Title: Title was not descriptive.
Answer
Discuss

Discussion

How do you select more than one control in a userform? If you tell us how you do it with key strokes we'll tell you how to read the result from the screen.
Variatus (rep: 4889) Jan 19, '21 at 7:05 pm
Check out my updated answer, I think it does what you are trying to do.
don (rep: 1989) Jan 22, '21 at 10:53 am
Add to Discussion

Answers

-1

Not sure what you mean with selection set, but I think this tutorial might work (its on this site):

Getting Data from a UserForm

The Option Buttons section might be the most help as far as the loop is concerned since that part loops thorugh all controls in the UserForm.

If this doesn't help, then edit your question and provide a bit more information on what you are trying to do.

Update

If you just want to move the controls, then reference them however you want and change their .Left and .Top properties. 

You could do something like this:

Private Sub CommandButton1_Click()

TextBox1.Left = TextBox1.Left + 10

End Sub

That is testing with a UserForm that has 1 text box called TextBox1 and one command button and it moves the text box over 10 each time you click the button.

Discuss

Discussion

i have code to cycle through all controls but not 'Selected' controls. does that make sense?
ExcelStudent Jan 19, '21 at 3:05 pm
Thanks for updating the question but I haven't done that so can't help (and its getting late!).

Note that I deleted my answer (it should have been an addition to this thread). 
John_Ru (rep: 6142) Jan 19, '21 at 3:26 pm
What is a selected control? Can you even select multiple controls at once? I have a feeling that you are in the Visual Basic Editor and selecting multiple controls there and wanting the same thing to happen when the user runs the userform, but there should only be one control on focus at a time when the userform is running.
don (rep: 1989) Jan 20, '21 at 11:01 am
Or are you designing a user form and want to nudge several selected items  within the form design (rather than dragging them with a mouse) where Ctrl+ direction arrows works I think? 
John_Ru (rep: 6142) Jan 20, '21 at 11:56 am
Yes, i am designing a userfrom and want to nudge several (or one) controls in a particular direction. i want to do this programatically in VBA code. so is that something that can be done?
ExcelStudent Jan 20, '21 at 1:38 pm
ExcelStudent

Thanks for the clarification that you are talking about moving controls when designing a form. (it would be good if you added that to the original question for when others read it/ any answers). Is Nudge a third party VBA tool?

I'm afraid I can't help this time- my needs for designing forms are basic so I can do all I need by drawing a box with the cursor (to select several) then using  right click/ Make Same Size (options) and/or  Align (options). If controls are to be in a line, then View / Properties Window lets you give selected controls a common value (e.g. Top=90) or change a common a little (e.g. to Top=91).

Hope someone else knows how Nudge can operate.
John_Ru (rep: 6142) Jan 20, '21 at 3:16 pm
so an update to my question. i can put a command button on a form and during run time, move a combobox to the left or right. but during design time, it appears as if there is no way to do this. my last and failed attempt was to put this in the immediate window and run it.
userform1.combobox1.left = 0

this did nothing to the combobox on the form (during design time). so my guess is that you can't do this. going to close the question after one final attempt.
ExcelStudent Jan 25, '21 at 6:41 am
Think you're on a loser here sadly. I too can move controls in run time but not in design time (via macro). Suggest you resort to aligning or grouping and moving.
John_Ru (rep: 6142) Jan 25, '21 at 10:11 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login