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

51-60EXCEL VBA - Step-By-Step Guide To Learning Excel Programming Language For Beginners (Excel VBA Programming, Excel VBA Macro, Excel Visual Basic)

The document discusses building a calculator application in VBA using forms. It provides instructions on setting up a userform, including changing its name and caption. The first step is to insert a userform into a new blank Excel workbook and VBA project. Various controls like labels and buttons will be dragged onto the form to build the calculator interface. The document emphasizes best practices like choosing descriptive variable names, writing code in the correct order, and adding comments.

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)
351 views

51-60EXCEL VBA - Step-By-Step Guide To Learning Excel Programming Language For Beginners (Excel VBA Programming, Excel VBA Macro, Excel Visual Basic)

The document discusses building a calculator application in VBA using forms. It provides instructions on setting up a userform, including changing its name and caption. The first step is to insert a userform into a new blank Excel workbook and VBA project. Various controls like labels and buttons will be dragged onto the form to build the calculator interface. The document emphasizes best practices like choosing descriptive variable names, writing code in the correct order, and adding comments.

Uploaded by

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

Now, let’s do the process called “Calling”.

To do that will just write every


Module between its corresponding lines:

Private Sub cmdAddition_Click()


Addition
End Sub

Private Sub CmdDivision_Click()


Division
End Sub

Private Sub CmdSubstraction_Click()


Minus
End Sub

Private Sub CmdTimes_Click()


Times
End Sub

After you wrote the code above try pushing a button:

Welcome to your third bug!!

Why is this happening? Let’s read the message.


It says that it is not expecting a Module, but a Variable or Procedure.
The problem in this bug is that Modules and Procedures in this example are
called the same and that’s a big mistake. Nothing should have the same name
when programming!
Let’s fix it fast. Just add manually this time a number 1 after each sub
procedure in each module.

Public Sub Addition1()


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

You also should change the written code on Sheet1. Just add that one we’ve
just added to each sub procedure in the modules:

Private Sub cmdAddition_Click()


Addition1
End Sub

Private Sub CmdDivision_Click()


Division1
End Sub

Private Sub CmdSubstraction_Click()


Minus1
End Sub

Private Sub CmdTimes_Click()


Times1
End Sub

Try it now by pushing every button and see if it works!


Great!!
We are supposed to have a much better idea about Modules and Procedures.
We’ll always use them. However, maybe you noticed that some of them are
called Public and others are called Private.
What does it mean?

PUBLIC means that that procedure can be called from anywhere. You’ll even
notice that all of them are even in the Macros list, but those saying PRIVATE
aren´t.

Try its meaning by changing just one or two Modules, change the word
Public to Private, and try run it.
As it was a Private one, it couldn’t find it. Let’s change the word Private back
to Public, you’ll see that it will work.

So, Private ones can’t be called, Public ones can be called, even from the
Macros list. In other words, Privates can’t be linked, but Publics can.
We’ve seen the most basic functions of VBA. You should have a great idea
about VBA now. However, let’s do something much more professional: A
real calculator.

Adding Letters?

Before we start the other calculator, do this experiment which will be


interesting for you to know, and in the same time, very useful. In the last
calculator we’ve made try adding two letters instead of numbers and Add
them.
You’ll notice that you can add letters!! A + A = AA !!?? It shouldn’t work if
you make a subtraction, division or multiply.
It happens because using “ + “ in VBA is not the same than the formula
=SUM(). Let’s remember that by now, because it will very useful for the
next project.
Quiz 4

What is a Private Procedure?


a) It means no one can see the Code because it is hidden.
b) It means it can’t be copied and pasted
c) It can’t be called from another part of the code

What is a Public Procedure?


a) It means that it can be called and executed from any part of the
Workbook
b) It can be seen online
c) It means you can’t protect it with passwords.
CHAPTER 5
Project: Calculator using Forms
Review
This time we’ll use much more Visual Basic for Applications
functionalities. Now we will understand why it is called Visual Basic.

First than anything let’s review a few things important to consider:


1. Choose the correct Variables to avoid bugs and make the program run
smoothly.
2. Write code in the correct order, otherwise it will produce a bug.
3. Never repeat the name of anything in a program. Everything should be
identified with a unique name.
4. Hope you noticed blue letters. They are built-in codes used by Excel
VBA. Don’t erase them or change them without knowledge, or it will
produce errors.
5. TIP: Always add names using at least a Capital, and when you call
them write their names using lowercase. You’ll notice that using this
way you’ll avoid typing mistakes, because if you wrote it correctly it
will add Capitals automatically, otherwise it will remain the same.
6. Constantly try the new code you’ve written, so you’ll be sure that it is
working fine.
7. Add enough comments to let you know what your code does in case
you need to review anything in the future. We’ll learn how to do that.

FORMS

Visual Basic for Applications in Excel has a very attractive feature called
Forms in which we can create a visual application like that we saw in the
beginning of this book. That’s how we’ll create the calculator.
Follow these steps:

1. Open a New Blank Excel Document.


2. Open Visual Basic.
3. Click on Insert TAB.
4. Userform.

You should see something like this:

That’s an userform. It will be used to create a Calculator this time. First, you
see a Box called “Controls” with several options inside. We’ll use it almost
all time, so don’t close it. In case you do it, ou can open it again by clicking
on View TAB, then on Toolbox.
Don´t close the properties Userform either. This will be the main tools
we’ll use for this project, and In this case, we won’t even touch a Excel
Spreadsheet.
In the properties window change these values:

Name: CalculatorProject
Caption: Calculator

Height: 260
Width: 200

Remember that Caption is the Displayed Title.


In the toolbox select the label, it is an A next to the Arrow.

Once it is selected, click on the upper side of the form, hold and drag to make
a rectangle which will be our screen for the calculator.

You might also like