(供将来参考)是否可以做这样的事情
For i = 0 to 11
array(i) = Label(i)
Next
而不是通过制作12行类似的粗暴强制它 array(0)= Label0,array(1)= Label1等
答案 0 :(得分:0)
是的
例如
Private Sub CommandButton1_Click()
ReDim myArray(0 To Me.Controls.count - 1) As MSForms.Label '<~~ declare this array with the maximum possible dimensions
Dim lblCounter As Long
Dim ctrl As MSForms.Control
With Me '<~~ this refers to the UserForm object of which code pane you placed this code into
For Each ctrl In .Controls '<~~ loop through UserForm controls
If TypeName(ctrl) = "Label" Then '<~~ if you find a "Label"...
Set myArray(lblCounter) = ctrl '<~~ ... then store it in your array...
lblCount = lblCounter + 1 '<~~ ... and update the label counter
End If
Next ctrl
End With
If lblCounter > 0 Then ReDim Preserve myArray(0 To lblCounter - 1) '<~~ if you actually found at least one label, then redim your array with proper dimensions
End Sub
当然,您可以将其放置在可以访问您的Userform Controls
集合的任何事件sub / eventhandler中