Total Pageviews

Tuesday 22 January 2013

Handling Many controls in a VBA userform



'If you have a form with many text box and other controls almost doing the same job then  you could use the following code (more like control arrays ) to handle the value of many control in one single looping statement.

Code :

'Code - watch immediate window for results.
Dim ObjControl As Control
 For Each ObjControl In Me.Controls
           If TypeOf ObjControl Is MSForms.Label Then  '\\\To loop through just Lables in a Form
                Debug.Print ObjControl.Name
           ElseIf TypeOf ObjControl Is MSForms.TextBox Then '\\\To loop through just textbox in a Form
                Debug.Print ObjControl.Name
           ElseIf TypeOf ObjControl Is MSForms.CommandButton Then '\\\To loop through just buttons in a Form
                Debug.Print ObjControl.Name
                '\\\similarly you can handle multiple controls in form without referring it each time
           End If
 Next


No comments:

Post a Comment