When you run your code this is the message box with the icon and three buttons that should appear, as shown below. This will make long code easier to read by putting it onto multiple lines. To add the code to several lines, press the spacebar then an underscore and then hit enter. MsgBox "The value was added successfully", _
For now I just want to demonstrate how to add the various icons and buttons to our test message box. In a moment I will show you how to add code that will make these buttons very effective communication tools. In the message box below we have chosen the vbYesNoCancel constant which will add three buttons to a message box. To create effective communication within our applications we need to add buttons and icons and If or Case functions that will enable the user to make a successful choice or decision. Here are the four constants.Įight buttons are available to us in the six constants displayed below. Note: I do not suggest that you use numbers as it will be difficult to decipher your code. Notice the information icon in the message box. Now click inside your code and push the F5 key to run the procedure. MsgBox "The value was added successfully", 64 MsgBox "The value was added successfully", vbInformation Select vbInformation from the drop-down list. We are being told here exactly what to do step-by-step. Test this out your code should look just like this. Now type the keyword MsgBox, after you typed the keyword MsgBox and push the space bar you are prompted with all of the options available to the message box. We are going to step through the available parameters one at a time.ĭelete the line: MsgBox "The value was added successfully". The syntax for the message box function is very interesting and extremely versatile. MsgBox "The value was added successfully" Put the mouse pointer inside the code and press the F5 key to run the macro. We will add a value to range A1 of the active worksheet and then tell the user that the operation was successful with a simple message box.Ĭopy and paste this into the module you created earlier. Test each piece of code until you fully understand the wonderful features available. Into this module we will put the test code that you see this article. Open the Visual Basic Editor and insert a new module from the Insert Tab. Let’s see how we can create a simple message box like this with Visual Basic for Applications. The Action is stopped and there is a warning, but more importantly the user is helped to make the right decision. Notice what happens as we try to copy across merged and unmerged cells at the same time. Microsoft Excel communicates with us with message boxes when actions that we take are inappropriate and to explain how to make the right decision with regard to the wrong action. In this chapter we will discuss how we can use message boxes to communicate and facilitate decision-making within our applications. Our applications should not only hold out their hand to greet the user but should also communicate with them where necessary to ensure that the right decisions are made. Let your applications speak for themselves Video content for VBA Message Box Constants available to the message box function.Microsoft Excel’s use for message boxes.Let your applications speak for themselves.