UserForms UserForms are a tool for providing graphical interaction to an Excel spreadsheet. See, for additional materials, http://www.fontstuff.com/vba/vbatut09b.htm. In this document we will go over some of the basic steps and ideas. Here is a picture of a basic UserForm in action: This user form has a window (called a “TextBox”) into which a user can type data, two buttons (labeled “OK” and “bye”), a “spin control” (the gadget at the left that looks like two triangles), a YES/NO checklist, and an image. The colors can be customized (though you will agree that our choice is artistic) and the form can be moved around like any window. This form takes inputs in many forms: in addition to the TextBox just described, the user can click any of the buttons, as well as the spin control. Each such input is “captured” as a “signal” by the VBA environment; for each such input we will write a corresponding piece of VBA code that takes appropriate action. For example, when the user clicks on “bye” the UserForm will go away. And when the user clicks on “OK” then some computation takes place. So: we create the UserForm. We also create the pieces of code that handle the inputs that the form provides. What we don’t do is to provide the “wiring” that connects actions by the user (i.e., clicking on a button) and the corresponding VBA code – the environment makes sure that our code will automatically be invoked. Let’s create a user form from scratch. From Excel, type Alt+F11, and then go: Insert > UserForm. You should get a picture like this, showing a “blank” or “empty” UserForm: You may also get more, depending on how Excel was last used. In fact, in order to change the attributes of the form, we need to get a little more. Right-click on the form, and select properties. You’ll get: The panel on the left allows you to change properties of the form, for example, colors, fonts, Caption (what is on the title bar), and, in particular, the name. Here’s what we got after some changes: Here, we renamed the form “testform”. From now on, we can use the name “testform” from anywhere in our VBA code so as to refer to this particular form. For example, from any Sub or Function we could “launch” testform. Alternatively, we can use a button on the Excel spreadsheet to launch testform. This is done as follows. The button we will use is not a “normal” button, of the form we saw before in the course – it is instead a “Command button”. Under Excel 2007, you click on the office button, then Excel Options, and in the “Popular” category under “Top options for working with Excel,” select “Show Developer tab in the Ribbon.” Then from the Developer tab, click “Insert”, and under “ActiveX controls”, click on the upper leftcorner button. Also see: http://office.microsoft.com/en-us/excel/HP102366761033.aspx. Under Excel –pre 2007, to create such a button, right click on any tool bar, and then select “Control Toolbox”, which will give you: Then: click on the button, and then click anywhere on the spreadsheet. In either case you will get a button labeled “Command Button1”. It can be moved, resized, etc. Further, in the Control Toolbox you click on the middle item on the top row, you will able to access other properties of the button, such as its color, caption, font, etc., as well as its name. In this case, I’m assuming that we renamed the button “launcher” (note: the name is not the same as the button caption, which is what a user will see). The button name is what will be used by the rest of our program to handle this button. Right-click on the button, and then select “View Code”. This will activate the Visual Basic editor, and you will see the following picture: The code that we see here, for a sub named “launcher_Click()”, is code that was automatically generated for us just now. This code will be automatically called each time a user clicks on the command button. Notice that right now, the code does absolutely nothing – but by inserting appropriate commands inside this Sub we will be able to make our program take appropriate action anytime a user clicks on the command button. In our case, what we type is: testform.Show so that the overall code that we have is: Private Sub launcher_Click() testform.Show End Sub Recall that “testform” is the name of the UserForm we created. The command we added will simply create, and display, the UserForm. In order to test this capability, we must leave “Design Mode” which is the special state of the VBA editor under which we created the command button. On Excel pre-2007, make sure that you see the Control Box. Then click on the top left item so that you exit the “Design Mode”. In Excel 2007, go to the Designer tab, and click on the same icon as above. Then click on the command button. It should make our testform appear. Right now, the user form is blank. Let’s add some capabilities to it. In particular, we want to be able to interact with the data in the spreadsheet. Close the control toolbox. Then go back to the Visual Basic editor, and if you don’t see the testform, then in the “Project” window on the left, under “Forms”, select testform. Then you should see the User Form, plus a “Toolbox”: If you don’t see the Toolbox, click on the hammer and wrench icon (see the figure above) to make it appear. In the toolbox, click on the “SpinButton” and then click again on the UserForm. At this point you will have a SpinButton as a member of your UserForm. In this example, we will endow the SpinButton with the following functionality: 1. When the user clicks on the up arrow, the entry in cell A1 is increased by 0.5 2. When the user clicks on the down arrow, the entry in cell A1 is decreased by 0.5 Of course, for this functionality to be really make sense we would need to make sure that cell A1 contains a number – let’s put this aside for now. To create this functionality we will use the VB environment to generate “signal handlers”—these are Sub procedures that will be automatically called when the user clicks on one of the two arrows. In the VB editor, Right-click on the ‘testform’ entry, and then left-click on ‘View Code’. Usually, you will see the following: Click on the down-arrow next to (General), and select SpinButton1. Now you get: instead of (General) in the picture above, and then you use the drop-down menu to select the Spin Button. Click on the down-arrow at the right end of the window that reads Change, and select “SpinDown”. Then repeat, but this time select “SpinUp”. The code we see is: Private Sub SpinButton1_Change() End Sub Private Sub SpinButton1_SpinDown() End Sub Private Sub SpinButton1_SpinUp() End Sub What we have here is three sub procedures that will be automatically called as a response to user actions. SpinDown and SpinUp correspond, respectively, to clicking on the down and up arrows of the SpinButton. We will modify these two procedures. For now, we will ignore SpinButton1_Change(). We change the code as follows: Private Sub SpinButton1_SpinDown() Range(“A1”).value = Range(“A1”).value – 0.5 End Sub Private Sub SpinButton1_SpinUp() Range(“A1”).value = Range(“A1”).value + 0.5 End Sub (the red text is what we typed). spreadsheet! Save your work, exit the VB editor, and try the
© Copyright 2026 Paperzz