问题
I have a userform with a bunch of frames. I accidentally dropped a checkbox into one of those frames and it is now nowhere to be found. It still exists, because I'm able to set the value of it with the code, but I can't see it. I tried expanding the frame and also moving the things in the frame (in case it went behind one of those controls). How can I pull it back out?
回答1:
This will tell you the position of the checkbox within the frame:
Private Sub UserForm_Initialize()
Debug.Print Me.CheckBox1.Left
Debug.Print Me.CheckBox1.Top
End Sub
After that, it's merely a question of expanding the frame in that direction and grabbing the "runaway child".
回答2:
Find wanted controls with parent names and positions
You could use this example code to locate systematically any types of controls:
Sub AnalyzeCtrls()
' Purpose: Analyze Userform controls
Dim ctrl As MSForms.Control
Debug.Print "Control Parent Position" & _
vbNewLine & String(76, "-")
For Each ctrl In UserForm1.Controls ' << change to your userform name
' display only "Checkbox" data ' << change to wanted search type
If TypeName(ctrl) = "CheckBox" Then Debug.Print ctrlInfo(ctrl)
Next ctrl
End Sub
Helper function
Private Function ctrlInfo(ctrl As MSForms.Control) As String
' Purpose: helper function returning userform control information
ctrlInfo = _
Left(TypeName(ctrl) & _
": " & ctrl.Name & String(20, " "), 20) & vbTab & _
" .." & IIf(TypeName(ctrl.Parent) = "UserForm", "Me " & String(15, " "), _
Left(TypeName(ctrl.Parent) & ": " & String(10, " "), 10) & _
Left(ctrl.Parent.Caption & String(15, " "), 15)) & vbTab & _
" Top " & Format(ctrl.Top, "# 000") & "/ Left " & Format(ctrl.Left, "# 000")
End Function
Example result in immediate window
Control Parent Position
----------------------------------------------------------------------------
CheckBox: CheckBox1 ..UserForm1:MyForm Top 006/ Left 570
Displaying the parent control helps you to find your lost control(s) there, e.g. a Frame, Multipage page or the userform itself.
来源:https://stackoverflow.com/questions/50862861/how-to-find-lost-userform-control