Userforms in VBA

This is a very short example of how to create a Userform to allow users to  input data into your work books. Userforms have the advantage of being professional looking and allow you to control the users interaction.


When in the VBA editor - right click - to add a Userform to your workbook.


On selecting the Userform you will greeted with this window.  




Here are the components of that window explained:
1 - Form area - this is what will be presented to the user.
2 - Toolbox - This is a collection of items you can add to your form.
3 - Project area - Here we can arrange our Userforms
4 - Properties box - Everything will have associated properties that can be adjusted to



Here we have created a label and changed the text to be "Name"



We have added a text box - this allows the user to input text into this box.

To allow us to get the value of this later we have renamed this to a more meaningful name.

Below we have entered a button - which allows the user to signal that they have entered the required information. 


Here we have updated the caption of the Userform.



Double clicking on an element we create function in the vba editor for that action.


Here we have added two lines of code. The first line takes the value that has been entered and moves it to cell "A1". The second line hides the form as the user is finished with it. 


Pressing f5 runs the script and displays the Userform.

We can enter our name and press the Button. This adds this to the sheet as expected and closes the Userform.






Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.



Must Read Articles


VBA and Microsoft Excel - Getting the most out of excel with VBA programming
VBA and Microsoft Word - Getting the most out of word with VBA programming
Application Object - Using the applications features in your code.
Installing VBA and First Program - What do you need to start using visual basic application VBA.