Visual basic for excel
You have to click the OK button to dismiss the message box and let the program continue - or in our case come to an end. You will also notice that the message box has an OK button and a title bar. In this case simply select the hello program and click the Run button.Īs long as you have entered the three lines correctly you should see the message box displaying “Hello World - Excel VBA here”. Next you should see a dialog box that gives you the choice of which program to run - in general a module can contain more than one program. It should be on by default but some one might have turned it off.Īlternatively you can use the command Run,Start or just press F5 - yes there are at least three different ways to run a program! If you can’t see the VBA Toolbar then use the command View,Toolbars and select the Visual Basic option. To run the program you can simply click on the green “play” arrow in the Visual Basic toolbar. Now the time has come to try your first program out. (including the quotes around the message).
So now type, in between the first and last line of your program the command MsgBox “Hello World - Excel VBA here” MsgBox is short for “Message Box” and whatever text you type between the quotes - which have to be included in the instruction - will be displayed in a message box. The simplest such instruction is MsgBox “ message” So what we need is an instruction that lets us display something on the screen. The key idea is that a program is read as if it was a list of instructions to be obeyed - which is of course exactly what it is. Now we have the start and ending lines of a valid VBA program the only problem is - it doesn’t do anything. OK so now we have the first and last line of our program and a name Step Four - Getting the message Once you understand the editor you will find that you can work with it rather than against it. For example if you don't notice that the editor has added End Sub and you go and add it again. This is great - except when it goes wrong. This is a general behaviour and the editor will try to help you as much as possible. You may also be surprised to find that VBA adds the brackets at the end of Sub Hello if you leave them off - they just have to be there! Similarly it automatically adds the End Sub once again it is essential - you can't have a Sub without and End Sub so the editor adds them for you. The word hello on the other hand isn’t a VBA keyword because it's up to you what you call a program. To show that it has detected a key word it turns the word blue. This is because they are VBA keywords - a fixed set of commands that VBA recognise. If you enter these lines you will notice that the Sub and End Sub automatically appear in blue and with the correct capitalisation. The final line of the program has to be End SubĪnd leaving this off makes Excel think you haven’t finished your program.
Sub is short for Subroutine but more of this later.įor example in our case the program would start Sub hello()Īnd the program would be called hello. Where name is the name you want to give your program. Now we have to enter our first simple program.Īll programs in VBA have to start and end in a particular way.