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

Excel For Eng 02 PDF

Uploaded by

Rami Majdi
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 views21 pages

Excel For Eng 02 PDF

Uploaded by

Rami Majdi
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/ 21

Excel for Engineers Part 2

1-Macros in Excel
2-User defined functions
3-Controls from form Toolbar
4-User form and controls
5-VBA (Visual Basic for Applications)

Excel for Engineers-part2 1 Excel for Engineers-part2 2

How to record a macro Macros in Excel

1-Go to tools→macro→ record a new macro


2-Go to cell B6 and type x, and type y in cell C6
3-Write the numbers 1 to 10 in cells B7 to B16

1-Macros in Excel 4-Go to cell B18 and type =sum(B7:B16)


5-Go to Cell C7 and write =B7^2
6-Copy from cell C7 to C16
7-stop recording the macro by pressing the stop button

Now go to visual basic editor (i.e. press Alt+F11)


And then click on module
You will see the macro that you have created

Excel for Engineers-part2 3 Excel for Engineers-part2 4

1
Record a macro Macros in Excel Record a macro Macros in Excel

Excel for Engineers-part2 5 Excel for Engineers-part2 6

Record a macro Macros in Excel Record a macro Macros in Excel

Excel for Engineers-part2 7 Excel for Engineers-part2 8

2
Record another macro Macros in Excel Assign a macro to a command button Macros in Excel

How to create a command button on the


excel sheet and link it to a macro

1-After creating a button


2-Go to view→toolsbar→forms
3-A small window will pop up showing some of the
controls that you can place on the excel sheet
4-Choose the Command button and place it some
where in the excel sheet
5-Once you release the mouse another window will pop
up asking you to attach or link the command button you
have created to the macro available
Excel for Engineers-part2 9 Excel for Engineers-part2 10

Assign a macro to a command button Macros in Excel Assign a macro to a command button Macros in Excel

Excel for Engineers-part2 11 Excel for Engineers-part2 12

3
Assign a macro to a command button Macros in Excel Assign a macro to a command button Macros in Excel

Excel for Engineers-part2 13 Excel for Engineers-part2 14

Form controls

Form controls

You can create scroll bar, list_box,


combo_box and others on Excel
sheet using the form controls

Excel for Engineers-part2 15 Excel for Engineers-part2 16

4
Form controls
Form controls
Group box

Command
button

Combo Box

List Box
Check
Box
Scroll Bar

Option button
Excel for Engineers-part2 17 Excel for Engineers-part2 18

Form controls Form controls

Excel for Engineers-part2 19 Excel for Engineers-part2 20

5
Form controls

msgBox and inputBox

Excel for Engineers-part2 21 Excel for Engineers-part2 22

InputBox Function
InputBox Function
Syntax
InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])

Value=inputBox(Prompt, title, default)

Prompt is a text ( i.e. it must be


written between two quotes)
Title is also a string

Value1=inputBox(“Enter A value”, “Heat


Transfer”,111)

Excel for Engineers-part2 23 Excel for Engineers-part2 24

6
Input Box

Massage Box

Syntax

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

Excel for Engineers-part2 25 Excel for Engineers-part2 26

User Defined Functions

Excel for Engineers-part2 27 Excel for Engineers-part2 28

7
Creating user defined functions User defined functions User defined functions

To access the visual basic editor


tools→macros →visual basic editor
Or just type Alt-F11
You will see the visual basic
Go to insert and insert a module
You can view the project ad see that a
module is added to the project. In the
module now you can add functions and
subroutines

Excel for Engineers-part2 29 Excel for Engineers-part2 30

User defined functions User defined functions

Suppose we want to add a function


that do the followings

y = a0 + a1 x + a2 x 2
Function myfun(x)
a0=5
a1=0.5
a2=0.75
myfun=a0+a1*x+a2*x^2
End function

Now you can go to the excel sheet and type =myfun(1) the
answer will be 6.25
Excel for Engineers-part2 31 Excel for Engineers-part2 32

8
User defined functions User defined functions

Select a cell and type


=myfun(1)

Excel for Engineers-part2 33 Excel for Engineers-part2 34

Notes on creating user defined functions


To see all user defined functions and
subroutines 1-Try to document every function you write. So
that when you come back to it after a while you
can remember what was the purpose of the
go to tools→macro→visual basic editor function and the source of it
And press on module 2-if you start the first line with single quote or the
word rem, then that statement is a remark
statement
3-use the command debug.print to send values
of the variable to immediate window
4-Keep all related functions and subroutines in
separate module.
Excel for Engineers-part2 35 Excel for Engineers-part2 36

9
Example on creating functions

Another Example of creating A user


defined functions

The effectiveness for counter current flow


heat exchanger

Excel for Engineers-part2 37 Excel for Engineers-part2 38

List of the function eff_counter

Function eff_counter(NTU, Cr)


' This function finds the heat effectivness for
' counter current heat exchanger
' The input parameters
' 1-NTU is the no of transfer unit
' 2-Cr is the capacity ratio=Cmin/cmax
'
' C=m_dot*Cp
' NTU=Cmin/UA
' the output is the heat exchanger effectiveness
If Cr = 1 Then
Cr = 0.999
End If
x1 = 1 - Exp(-NTU * (1 - Cr))
y1 = 1 - Cr * Exp(-NTU * (1 - Cr))
eff_counter = x1 / y1
End Function
Excel for Engineers-part2 39 Excel for Engineers-part2 40

10
Steps to insert a module into a
Modules can be saved and worksheet
insert in new worksheet
1-In a new or current workbook go to Visual
Basic editior (Alt-F11)
Steps to export a module
2-Insert a new module
Go to visual basic editor
3-Click on the module with mouse right click
Choose the module to export select import file
With mouse right click select Export file 4-A pop up window will appear to select the
module you want to insert
Choose the prefer name and location to
save the module file [The extension of the 5-Choose the location and the name of the file
file will be *.bas]. It can be several (module) you want to insert
functions and modules
Excel for Engineers-part2 41 Excel for Engineers-part2 42

Inserting a module into excel sheet

Some Useful Applications

1-Table interpolation
2-Friction factor in pipes
3-Moist air properties
4-Thermodynamic properties
of water

Excel for Engineers-part2 43 Excel for Engineers-part2 44

11
1- Table interpolation

1- Table interpolation

Program name:
table interpolation.xls

Excel for Engineers-part2 45 Excel for Engineers-part2 46

1- Table interpolation 1- Table interpolation

Excel for Engineers-part2 47 Excel for Engineers-part2 48

12
1- Table interpolation

Air properties 2- Friction factor for flow


inside pipes
Cp_air(T)
Prandtl_air(T)
Density_air(T)
Program name f_factor.xls
Conductivity_air(T)

Water properties
Cp_water(T)
Prandtl_water(T)
Spvolliq_water(T)
Conductivity_water(T)
Excel for Engineers-part2 49 Excel for Engineers-part2 50

2- Friction factor for flow


2- Friction factor for flow inside pipes
inside pipes

Laminar 64
flow Re<2300 f =
Re

Turbulent 1 ⎡e / D 2.51 ⎤
Re>=2300 = −2.0 log ⎢ + 0.5 ⎥
flow f 0. 5
⎣ 3.7 Re f ⎦

Excel for Engineers-part2 51 Excel for Engineers-part2 52

13
2- Friction factor for flow
inside pipes
3-Moist air properties

Dry bulb temp


Wet bulb temperature
Total pressure
Humidity ratio
Enthalpy
Relative humidity
Dew point temperature

Excel for Engineers-part2 53 Excel for Engineers-part2 54

3-Moist air properties

3-Moist air properties

humidity ratio
Given two thermodynamic properties of air
along with total pressure, one then can find the h
rest of the properties W h Ws

f
For example Ws
*

Given tdry, twet, air pressure pa W


1
Find h, RH, dew point temp. sa
t.
t
*
humidity ratio etc

dry temp. td t

Excel for Engineers-part2 55 Excel for Engineers-part2 56

14
3-Moist air properties
Some of the moist air properties relation

W = 0.622 Pv
P - Pv
*
Ws ( 2501 - 2.326 t * ) - (t - t * )
W=
(2501 + 1.86 t - 4.186 t * )
WP
Pvs ( t d ) =
0.622 + W

φ = Pv
Pvs
h = C pa * t + W (2501 + C pv * t ) = t + W (2501 + 1.86 * t )

ln( Pvs ) = C1 /T + C 2 + C 3 T + C 4 T 2 + C 5 T 3 + C 6 T 4 + C7 ln(T)


Excel for Engineers-part2 57 Excel for Engineers-part2 58

3-Moist air properties

4-Water thermodynamic properties

File name:
Xsteam_excel_v2.6.xls
Site: http://www.x-eng.com

Excel for Engineers-part2 59 Excel for Engineers-part2 60

15
4-Water thermodynamic properties
4-Water thermodynamic properties
T

CP
Steam and water thermodynamic and P2 > P1
thermo-physical properties
P1
Sub-cooled region
Sub-cooled region
Saturated region Super heated region
Saturation region
Saturated vapor line
Superheated region
Saturated liquid line

s
Excel for Engineers-part2 61 Excel for Engineers-part2 62

4-Water thermodynamic properties 4-Water thermodynamic properties

Excel for Engineers-part2 63 Excel for Engineers-part2 64

16
4-Water thermodynamic properties 4-Water thermodynamic properties

Given the pressure P ( in bar)

Saturated temperature (Tsat) : Tsat_p(P)


Saturated liquid enthalpy hf: hl_p(P)
Saturated vapor enthalpy hg: hv_p(P)
Saturated liquid specific volume vf: vl_p(P)
Saturated vapor specific volume vg: vv_p(P)

1 bar=100 kPa
Excel for Engineers-part2 65 Excel for Engineers-part2 66

4-Water thermodynamic properties 4-Water thermodynamic properties

Given the temperature T ( in deg. C) Superheated steam properties given P & T

Saturated pressure (Psat) : Psat_p(T)


1-Enthalpy h: h_pt(P,T)
Saturated liquid enthalpy hf: hl_t(T)
2-Entropy s: s_pt(P,T)
Saturated vapor enthalpy hg: hv_t(T)
3-Density rho: rho_pt(P,T)
Saturated liquid specific volume vf: vl_t(T)
Saturated vapor specific volume vg: vv_t(T)

Excel for Engineers-part2 67 Excel for Engineers-part2 68

17
4-Water thermodynamic properties

User form and creating a user interface

1-Go to Visual Basic editor


2-Insert a userform
3-On the userform insert tools
4-Change the properties of each tools as
required
5-Program other tools such as command
button, listBox, ComboBox, etc

Excel for Engineers-part2 69 Excel for Engineers-part2 70

Insertion of a form Insert controls on the userform

ComboBox
Command
button

Label Tools
Forms, user Toolbox for
form1 Controls

Text Box Userform


Properties window frame
Properties
Userform window

Excel for Engineers-part2 71 Excel for Engineers-part2 72

18
Example: Simple Program

Construct a program with user interface


to add two numbers and show the result

Excel for Engineers-part2 73 Excel for Engineers-part2 74

Excel for Engineers-part2 75 Excel for Engineers-part2 76

19
Excel for Engineers-part2 77 Excel for Engineers-part2 78

Summary
1-Cell Referencing (Relative & absolute) Conclusions & Recommendations
2-Introducing some of the built in functions &
Extend your use of Excel 1-Excel is handy, available and powerful
3-Use of Goal & Seek and Solver 2-Lots of help and examples from the net
4-Macros in Excel 3-It is highly recommended to extend you
5-Forms toolbox capability of using Excel for your courses

6-User defined functions 4-Learn how to use new functions, build your
own functions and form modules
7-Usful Engineering Applications
5-For full excel usefulness learn VBA
8-Userform and VBA

Excel for Engineers-part2 79 Excel for Engineers-part2 80

20
Excel for Engineers-part2 81 Excel for Engineers-part2 82

References for Excel uses and


VBA programming

1-Excel for Engineers


2-Internet (lots of sites, examples and
forums

Excel for Engineers-part2 83 Excel for Engineers-part2 84

21

You might also like