Está en la página 1de 5

VBA–ControlArrays

7
Posted on: 08-5-2011 by: Siddharth Rout

These are the few things that we will be covering in this post:

What is a Control Array?

Can we create Control Arrays in VBA?

Why do we need a Control Array?

Examples of Control Arrays in VBA
a) Using existing textboxes in the UserForm and assigning a same set of event procedures
b) Creating new textboxes at runtime and assigning them same set of event procedures

WHATISACONTRO LARRAY?
A Control Array is a group of controls that share the same name type and the same event procedures. They
are a convenient way to handle groups of controls (Same Type) that perform a similar function. All of the
events available to the single control are available to the array of controls.

CANWECREATECONTRO LAR RAYSINVBA?
Even though, VBA doesn’t allow us to to create Control Array like in vb6 and vb.net, we c an still create
Control Array in VBA.

WHYDOWENEEDCONTRO LAR RAYSINVBA
Control Arrays mainly have these advantages:

Controls in a Control Array share the same set of event procedures. This results in you writing less amount of
code.

Control Arrays uses fewer resources.

You can effectively create new controls at design time, if you need to.

EXAMP LESO FCONTRO LARR AYSINVBA
We will be covering the following here:
 a) Using existing textboxes in the UserForm and assigning a same set of event procedures
 b) Creating new textboxes at runtime and assigning them same set of event procedures
Using existing textboxes in the UserForm and assigning a same set of event proc edures.
Let’s say you have 10 textboxes in your UserForm (see image below):

you would have a code like this: Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.And you want all 10 to be numeric textboxes. See the two images below. 8 Case Else KeyAscii = 0 End Select End Sub Now imagine writing this code 10 times for each and every textbox? This is where we will use Control Array of Textboxes and assign them the same procedure. Numeric textboxes are those text boxes where you can only type numbers. You can do that by right clicking on the “ VBAProject → Insert → Class Module“. If it was just 1 TextBox. . add a new Class.ReturnInteger) Select Case KeyAscii '<~~ 48 to 57 is ASCII code for numbers. 127. 127 is for 'Delete' and 8 is for 'Backspace' Case 48 To 57. To start with.

TextBoxEvents = TBCtl End If Next TBCtl Set TBCtl = Nothing End Sub . 127 is for 'Delete' and 8 is for 'Backspace' Case 48 To 57.TextBox Then i = i + 1 ReDim Preserve TextArray(1 To i) Set TextArray(i). 127. TBCtl As Control For Each TBCtl In Me. paste this code: Option Explicit Dim TextArray() As New Class1 Private Sub UserForm_Initialize() Dim i As Integer. 8 Case Else KeyAscii = 0 End Select End Sub …and in the UserForm Initialize event.ReturnInteger) Select Case KeyAscii '<~~ 48 to 57 is ASCII code for numbers.Now paste this code in the code area of the Class1 Module: Public WithEvents TextBoxEvents As MSForms.TextBox Private Sub TextBoxEvents_KeyPress(ByVal KeyAscii As MSForms.Controls If TypeOf TBCtl Is MSForms.

click() etc. You will notice that you will not be able to type anything other than numbers or pressing the ‘Delete’ and the ‘Backspace’ button.TextBox Dim TBoxTop As Long.And you are done! Now when you run the UserForm. Now let’s take another scenario. Instead of creating textboxes at design time and then assigning them same set of event procedures. Similarly you can create other events for textboxes like change(). i As Long '~~> Decide on the . Paste this in the the Initialize event of the UserForm: Option Explicit Dim TextArray() As New Class1 Private Sub UserForm_Initialize() Dim ctlTBox As MSForms. Creating new textboxes at runtime and assigning them same set of event procedures. all the textboxes will now show the same behaviour. Let’s say your UserForm now simply looks like this: Create the Class module as I have shown above and then paste the code which I gave above for the Class module. To test it. simply run your UserForm and try typing anything in the textboxes.Top for the 1st TextBox TBoxTop = 30 For i = 1 To 10 . what we want to do is to create these textboxes at run time and then assign them same set of event procedures.

Add("Forms.TextBox.Left = 50 '~~> Increment the .Set ctlTBox = Me.1". This is how your UserForm will look: . simply run your UserForm.Left property here ctlTBox.TextBoxEvents = ctlTBox Next End Sub …and you are done :) To test it.Top for the next TextBox TBoxTop = TBoxTop + 20 ReDim Preserve TextArray(1 To i) Set TextArray(i). You will see that the TextBoxes automatically get created and you will not be able to type anything other than numbers or pressing the ‘Delete’ and the ‘Backspace’ button. "txtTest" & i) '~~> Define the TextBox .Top = TBoxTop: ctlTBox.Controls.Top and the .