Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

C) It Is Not, Excel Declares Them Automatically According To The Value Added

We are creating a simple calculator using ActiveX controls and VBA modules. Modules allow us to organize our VBA code and procedures. We add buttons for addition, subtraction, multiplication and division using ActiveX controls. Then we create corresponding modules named Addition, Minus, Division and Times. Each module contains a procedure that performs the respective calculation on cells A2, B2 and C2. Finally, we link each button's click event to its corresponding module procedure.

Uploaded by

Muhammad Iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

C) It Is Not, Excel Declares Them Automatically According To The Value Added

We are creating a simple calculator using ActiveX controls and VBA modules. Modules allow us to organize our VBA code and procedures. We add buttons for addition, subtraction, multiplication and division using ActiveX controls. Then we create corresponding modules named Addition, Minus, Division and Times. Each module contains a procedure that performs the respective calculation on cells A2, B2 and C2. Finally, we link each button's click event to its corresponding module procedure.

Uploaded by

Muhammad Iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

c) It is not, Excel declares them automatically according to the value

added.

Msgbox returns a value depending on the button pressed:


a) We assign the value to each button
b) It returns a value depending on the button pressed
c) Values aren’t returned
CHAPTER 4
Project: Creating a Simple Calculator using
ActiveX

We’re going to create a simple calculator first. After this project is


completed, we’ll create a more sophisticated one. To do that, we’ll require to
know the following:

What is a Module?
When we start writing VBA code, we’ll usually start writing on Sheet1. But,
in order to understand Modules, Procedures Private and Public, we’ll create a
visual calculator. First, we´ll do it on a spreadsheet, and then we’ll do it as a
real program.

A module, is something like a Box, in which we add some code to run when
we “call” it. To understand how it works, we’ll create our calculator using a
few Modules.

How to create a Module


First, add the following ActiveX buttons on a spreadsheet on the table as it is
shown on the image. To add the appropriate symbol to each button, it is not
as we did on the Form Buttons. In this case we have to click on the Design
More Button, which is in the developer TAB, then right click on each button
and you’ll see something like the image below, in which we see something
called “Name”. Most people would think that there’s where we can add the
correct symbol to see, but that’s wrong. Excel identify the Button with that
name. Imagine you have several buttons with + as their name. How would
either Excel or you identify which one is the correct? So, every button will
have a different name, let’s add a good one which let us know what it does
like In the example I wrote cmdAddition. It lets me know that that it applies

to the “+” Button. However, how can I add the sign +?

In the image above, you ‘ll see that there’s a property called Caption. That’s
where You’ll add the “+” sign.
You can even play a little bit with the other options, like the Backcolor, Font,
Height and even add a picture. I left all options as they were by default.

Once you repeated the process for all the buttons, let’s start with the next
steps. Let’s create a Module!
Follow these steps:

1. Open Visual Basic through the Developer TAB. By default you’ll see
something like this:

2. Click on “View TAB” and select “Properties Window”. We’ll always


use this window a lot. Now you should see something like what’s
shown inside the red square:
3. Let’s add our first Module. Click on Insert TAB, then Module.

4. You’ll see a New Folder Called “Modules” with a file there Called
“Module 1”:

5. Select it and in the Properties Window Change its name to Addition,


then repeat the process until you have created a Module for Minus,
Division and Times.
6. Double click on the Module “Addition” to open it.
7. You’ll see that it displays an entire blank sheet. We need to add a
Procedure! To do that, click on Insert TAB, then Procedure. It will
display a window like this, add a name and leave the default options:

8. Click OK and you’ll see this:

9. Between those lines write the following code:

Range ("A2") + Range("B2") = Range("C2")

10. Now run it by clicking the green triangle above.


11. It is displaying an error saying: Invalid use or Property.
Maybe it is because we haven’t added any number in the cells A2 and
B2. Add them and run it again.

It is still having a problem! Can you see what’s wrong?

Welcome to your second bug! The problem here is a very usual one. We are
asking Excel that cells A2 + B2 are equals to C2 instead of C2 equals to A2 +
B2. This problem is just an order problem. You better don’t forget this rule!!
Always add first the cell you want to be changed, then add the values that
you’ll need. Like this:
Range("C2") = Range("A2") + Range("B2")

Run it again with the same green triangle.


OK! It works!

If you see a yellow line which doesn’t let you run it, just click stop, correct
your code and run it again.

Now you’ll see that in the cell C2 we find the result of A2+B2.

Let’s complete the other modules repeating the correct process above. Add
these codes to do that:

Minus Module:
Public Sub Minus()
Range("C2") = Range("A2") - Range("B2")
End Sub

Division Module:

Public Sub Division()


Range("C2") = Range("A2") / Range("B2")
End Sub

Times Module:

Public Sub Times()


Range("C2") = Range("A2") * Range("B2")
End Sub

You’re supposed to see a file like this:


All the modules should work if you run them.

Now, let’s link the Modules to their appropiate button.

To do that just do the following:

1. Go to the spreadsheet
2. Click on Design Mode
3. Double click each button. You’ll see that every time you do it, it adds
some code to the Sheet1(Sheet1). Finally, it should look like this:

You might also like