Excel programming - an introduction
In this practical tip we give you an introduction to Excel programming with VBA (Visual Basic for Applications).
Excel programming: Create UserForm with VBA
First start Excel and press [Alt] and [F11] simultaneously to start the VBA editor. We also show you all the steps in the picture gallery at the end of the article.
- Click on the "Insert" tab and add a form by selecting "UserForm" from the dropdown menu. You can edit the name, height, width and other properties in the "Properties" window.
- You can then use the Tool Collection window to add various items. First, for example, add a "label". Change the text in this field by adjusting the "Caption" value in the "Properties" window.
- The next step is to add a "command button". Again, you can edit the appearance in the "Properties" window. If you now run this program and click the button, nothing would happen. To store commands with this button, simply double-click on it.
Excel programming: commands in VBA
In the following we will show you how you can assign various commands to your button.
- First, you can change the label of an existing label. For example, the command »Label1.Caption =" New label "« (without outer quotation marks) can be used. In this case, the labeling of the label is adapted to the name "Label1".
- You can also add commands that are executed when, for example, a key is pressed. You can select these different cases in the dropdown menu at the top right.
- The "MsgBox" Test "" command, with which a message with the content "Test" would be displayed, is also practical. This message "pops up" in a separate window. In the same way, you can use the »MsgBox x« command to display a message that contains data that has been stored in the "x" variable with a command such as "x =" Test "".
- Just like in other programming languages, you can calculate different values with commands like »x = 17 + 42« or »x = x + 1«.
Excel programming: text fields and conditions in VBA
Also important is the "text field", which you can also find in the "tool collection".
- You can store the entered value of this TextBox with the command »x = TextBox1.Text« for the variable "x".
- Conditions in VBA are also very practical. An example of this would be the command "If (TextBox1.Text = 42) Then". The next line shows the command that should be executed if the condition applies. However, the command to be executed if the condition applies must be moved to the right with the tab key. Further conditions must then begin with the "ElseIf" command. If none of the conditions apply, the command under »Else:« is executed. Don't forget to add the End If command at the end.
In the next practical tip, we will explain how quantum computers work.