0% found this document useful (0 votes)
187 views

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

This document provides an introduction to VBA programming in Excel 2007. It discusses how to add the developer tab, set macro security levels, open the VBA editor, and describes the programming environment. It then demonstrates how to add a new module and write simple functions to calculate sums and test precision. The document also presents analytical and numerical solutions to the parachutist problem and includes VBA code examples to calculate machine epsilon values in single and double precision. Finally, it provides additional tasks to create trigonometric function plots and solve the falling parachutist problem with an updated drag force formula.

Uploaded by

goragorago
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views

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

This document provides an introduction to VBA programming in Excel 2007. It discusses how to add the developer tab, set macro security levels, open the VBA editor, and describes the programming environment. It then demonstrates how to add a new module and write simple functions to calculate sums and test precision. The document also presents analytical and numerical solutions to the parachutist problem and includes VBA code examples to calculate machine epsilon values in single and double precision. Finally, it provides additional tasks to create trigonometric function plots and solve the falling parachutist problem with an updated drag force formula.

Uploaded by

goragorago
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lab 1

VBA PROGRAMMING IN EXCEL 2007

Dr. Majid Mohammadian


[email protected]
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