0% found this document useful (0 votes)
36 views7 pages

COMPROG MANUAL Topic-1-3

Here is the code to continue the 'getDaysOld' procedure: 1. Declare variables to store today's date, entered birthdate, and days difference Dim today As Date Dim birthDate As Date Dim daysOld As Long 2. Set today's date and get entered birthdate today = Date birthDate = InputBox("Enter your birthdate (mm/dd/yyyy)") 3. Subtract dates to get days difference daysOld = DateDiff("d", birthDate, today) 4. Display days old message MsgBox "You are " & daysOld & " days old." 5. BONUS: Calculate and display hours old Msg

Uploaded by

Shaine Inovero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views7 pages

COMPROG MANUAL Topic-1-3

Here is the code to continue the 'getDaysOld' procedure: 1. Declare variables to store today's date, entered birthdate, and days difference Dim today As Date Dim birthDate As Date Dim daysOld As Long 2. Set today's date and get entered birthdate today = Date birthDate = InputBox("Enter your birthdate (mm/dd/yyyy)") 3. Subtract dates to get days difference daysOld = DateDiff("d", birthDate, today) 4. Display days old message MsgBox "You are " & daysOld & " days old." 5. BONUS: Calculate and display hours old Msg

Uploaded by

Shaine Inovero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Page |1

TOPIC 1 – Introduction

VBA - Overview
VBA stands for Visual Basic for Applications an event-driven programming language from
Microsoft that is now predominantly used with Microsoft office applications such as
MSExcel, MS-Word, and MS-Access.
It helps techies to build customized applications and solutions to enhance the capabilities of
those applications. The advantage of this facility is that you NEED NOT have visual basic
installed on our PC, however, installing Office will implicitly help in achieving the purpose.
You can use VBA in all office versions, right from MS-Office 97 to MS-Office 2013 and also
with any of the latest versions available. Among VBA, Excel VBA is the most popular. The
advantage of using VBA is that you can build very powerful tools in MS Excel using linear
programming.

Application of VBA
You might wonder why to use VBA in Excel as MS-Excel itself provides loads of inbuilt
functions. MS-Excel provides only basic inbuilt functions which might not be sufficient to
perform complex calculations. Under such circumstances, VBA becomes the most obvious
solution.
For example, it is very hard to calculate the monthly repayment of a loan using Excel's built-
in formulas. Rather, it is easy to program a VBA for such a calculation.

Preparation of Microsoft Excel VBA

Activate developer tab in excel (Excel 2007 and up)


Go to “File”, then Click “Options”, then “Customize Ribbon” and check the “Developer” and
“OK” to enable the developer tab in excel.

Shortcut keys
Alt + F11 : Visual basic editor window
Ctrl + R Project Explorer
F4 Properties Windows
Page |2

Click new module


Type “sub groupname”
Type between Sub & End Sub
Range("a1") = 12
Then click play button (observe what happens on the excel sheet)
Next try
Range("a1:c6") = 12
Page |3

TOPIC 2 – Range Objects and Range Properties

1. Range object – same as cell in excel


Range object with cells

2. Range Properties:
a. .Value Property
MsgBox Range(“a1”).Value

b. .Text Property
MsgBox Range(“a1”).Text

c. .Row and .Column Property


MsgBox Range(“a1”).Text

d. .Select property
Range(“table_01”).Select

e. .Count Property
MsgBox Range(“table_01).Count

f. .ADDRESS PROPERTY
MsgBox Range(“a1”).Address(0,0)

g. .FORMULA PROPERTY
Range(“a1”).Formula = “=A2+A3”

h. .NUMBERFORMAT PROPERTY
Range(“a1”).NumberFormat = “0.00””mm”””

i. .FONT.BOLD, UNDERLINE OR ITALIC


Range(“a1”).Font.Bold = true
Range(“a1”).Font.Underline = true
Range(“a1”).Font.Italic = true
Page |4

EXERCISE 1
Objectives:
1. Create a Macro that types the following headers on row 1:
•ID
•First Name
•Last Name
2. Make the headers BOLD!

EXERCISE 2

Create a Macro that:


1. Enters the following data
2. Makes a1 through b1 BOLD
3. Selects cell C1
4. Now, manually create a shape that says Click Me and assign your macro to it. This is not part of
the macro, just make the shape manually.
Page |5

TOPIC 3 – Cell Objects


a. The Cell Object

Cells(1,2) = 50
Cells(1,”b”) = 50

b. Using Cells positionally within a Range


Cells(6) = 44
Range(“a1:c16”).cells(6) = 44

c. Affecting all cells in a worksheet


Cells.Font.Name = “Arial”
Cells.Font.Size = 15

d. Using Range Object with cells Object


Range(Cells(1,2),Cells(4,3)) = 4
EXERCISE 3
By default, Sheet1 on a worksheet will have a default cell font and will be zoomed to
100%.
In this exercise, the user wants a macro that formats a sheet to ideal conditions for
their work.
1. Format all cells as font "Arial"
2. Zoom in to
145%
3. Format Column D (4th column) as currency, because your company always has currency in
column D
Page |6

TOPIC 4 – Variables
a. Intro to Variables
Sub MyVar1()
MyVar = 50

b. Sub Var2()
Dim x As Integer, y As Integer
x = 10
y = 100
MsgBox "the value of x is " & x & Chr(13) & "the value of y is " & y
End Sub

c. Sub myVar3()
Dim hi as string
Dim hello As double
Dim mydate1 as Date

Hi = “Hello World”
Hello = 634.85
Mydate1 = “01/28/2019”
End Sub

d. Private and Public variables


Public myVar3
Sub MyVar1()
MyVar = 50
Call myVar3
MsgBox Hi
End Sub

e. Constant
Constant consname as string = “Bimby”
Page |7

EXERCISE 3

Continue with current procedure called 'getDaysOld' …


This procedure takes any Birthdate and reveals how many Days old someone is.
1. Take today's date minus the entered date to get difference (number of days since then)
2. Display "You are NNN days old" in a messagebox
3. BONUS! - Also display how many total hours old they are!

You might also like