C) It Is Not, Excel Declares Them Automatically According To The Value Added
C) It Is Not, Excel Declares Them Automatically According To The Value Added
added.
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.
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:
4. You’ll see a New Folder Called “Modules” with a file there Called
“Module 1”:
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")
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:
Times Module:
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: