Vba Programming in Excel 2007: Dr. Majid Mohammadian Majid - Mohammadian@uottawa - Ca

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Lab 1

VBA PROGRAMMING IN EXCEL 2007

Dr. Majid Mohammadian


majid.mohammadian@uottawa.ca
Adding the Developer Tab
 Click the Microsoft Office Button:
and then click Excel Options.
Click Popular, and then select the Show Developer
tab in the Ribbon check box.
MACRO SECURITY
Excel has several levels of Macro Security that can be
selected using Developer  Macro Security

 Select Enable all


 You can also create a self-signing
certificate
OPENING VBA EDITOR IN EXCEL 2007
Two ways to access the excel VBA programming
environment:
Alt F11
Using the Developer tab  Visual Basic
THE PROGRAMING ENVIRONMENT

Editor
Project Manager

Properties
ADDING A NEW MODULE
Select your spreadsheet in the project explorer
Use the main menu or the contextual menu (mouse
right button) to insert the new module
A simple function: my_sum
Open VBA using alt+f11
Type the following:

Function my_sum(a, b)
my_sum = a + b
End Function

Return to excel using Alt+q


Use your function
Save your file as Macro-Enabled (*.xlsm )
Single and double precision
Add the following functions

Function test_s()
Dim x As Single
x = 1.23456789012345
test_s = x
End Function

Function test_d()
Dim x As Double
x = 1.23456789012345
test_d = x
End Function
THE PARACHUTIST PROBLEM
Analytical solution:
gm   c m  t

v(t )  1  e
c  
Numerical (finite difference) solution:

 c 
v(ti 1 )  v (ti )   g   v(ti )   (ti 1  ti )
 m 
or
New value  Old value   slope   step size 
We are going to implement both solutions in Excel
ANALYTICAL SOLUTION
To calculate analytical solution using Excel, let
us first set up a simple spreadsheet. As shown
below, the first step involves entering labels and
numbers into the spreadsheet cells
To attach names to parameters values, select cells
Formula tab  Define Name

Create the time column:


D8=0
D9=D8+dt, Drag down

Type the following formula in F8


=g*m/cd*(1-exp(-cd/m*D8))
Drag down

Numerical: G8=0:
(Type in G9) =G8+(g-cd/m*G8)*dt
Function v2_parachute

Function v2_parachute(v1, dt, g, cd, m)


v2_parachute = v1 + (g - cd / m * v1) * dt
End Function
MACHINE EPSILON
Machine, or computer epsilon (eps) is the smallest floating point
number that can be added to a floating point 1 that yields a
result different from 1.
It is determined by the following type of algorithm
epsi=1
DO
IF(epsi+1 < 1 ) EXIT
epsi = epsi / 2
END DO
epsi = epsi * 2
print epsi
END
VBA CODE FOR MACHINE EPSILON
Function machineeps()
Dim epsi As Double
epsi = 1
Do
If 1 + epsi <= 1 Then Exit Do
epsi = epsi / 2
Loop
machineeps = 2 * epsi
End Function
Single precision
Function machineeps_s()
Dim epsi As Single
epsi = 1
Do
If 1 + epsi <= 1 Then Exit Do
epsi = epsi / 2
Loop
machineeps_s = 2 * epsi
End Function
Other Tasks
Task 6: Create a table where x are the values from -3.1415 to
3.1415, and the values of y are y= cos(x) without using VB. Plot
it. Add a title “y= cos (x)”, the axis titles x and y, and the legend.
Now add another column with values of y=sin(x). Try to add the
new series of values to the plot without plot the graph again
(Hint: See “Adding a new series of values” in Plotting ). Note:
Give your function in VB a special name e.g. my_cos (why?).

Task 7: Define the function y= cos(x) +sin(x) using VB, and plot
it from 0 to 3.1415.

Task 8 (To be submitted with assignment 2): Write a VBA


code for the falling parachute problem assuming that the drag
force formula is now FD is now FD=cV2. Submit a print of all
your VBA procedures and a table of v versus t using the
following data: c =0.125kg/s and

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy