Sei sulla pagina 1di 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

Excel for Engineers-part2

How to record a macro

Macros in Excel

1-Go to toolsmacro 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
4-Go to cell B18 and type =sum(B7:B16)

1-Macros in Excel

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

Excel for Engineers-part2

Record a macro

Macros in Excel

Excel for Engineers-part2

Record a macro

Macros in Excel

Excel for Engineers-part2

Record a macro

Macros in Excel

Excel for Engineers-part2

Record a macro

Macros in Excel

Excel for Engineers-part2

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 viewtoolsbarforms
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

Assign a macro to a command button

Excel for Engineers-part2

Macros in Excel

11

Excel for Engineers-part2

Assign a macro to a command button

Excel for Engineers-part2

10

Macros in Excel

12

Assign a macro to a command button

Macros in Excel

Excel for Engineers-part2

13

Assign a macro to a command button

Excel for Engineers-part2

Macros in Excel

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

Form controls

Form controls
Group box
Command
button

Combo Box

List Box
Check
Box

Scroll Bar

Option button
Excel for Engineers-part2

17

Form controls

Excel for Engineers-part2

Excel for Engineers-part2

18

Form controls

19

Excel for Engineers-part2

20

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

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

Creating user defined functions

User defined functions

User defined functions

To access the visual basic editor


toolsmacros 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

User defined functions

30

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

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
function and the source of it

go to toolsmacrovisual basic editor

2-if you start the first line with single quote or the
word rem, then that statement is a remark
statement

And press on module

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

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

10

39

Excel for Engineers-part2

40

Steps to insert a module into a


worksheet

Modules can be saved and


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


select import file

Choose the module to export

4-A pop up window will appear to select the


module you want to insert

With mouse right click select Export file


Choose the prefer name and location to
save the module file [The extension of the
file will be *.bas]. It can be several
functions and modules
Excel for Engineers-part2

5-Choose the location and the name of the file


(module) you want to insert
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

11

43

Excel for Engineers-part2

44

1- Table interpolation

1- Table interpolation
Program name:
table interpolation.xls

Excel for Engineers-part2

45

1- Table interpolation

Excel for Engineers-part2

12

Excel for Engineers-part2

46

1- Table interpolation

47

Excel for Engineers-part2

48

1- Table interpolation

2- Friction factor for flow


inside pipes

Air properties
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

Turbulent
flow

f =

Re<2300

Re>=2300

1
f

0. 5

64
Re

e / D
2.51
= 2.0 log
+
0.5
3.7 Re f

Excel for Engineers-part2

13

50

2- Friction factor for flow


inside pipes

2- Friction factor for flow


inside pipes

Laminar
flow

Excel for Engineers-part2

51

Excel for Engineers-part2

52

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

Given two thermodynamic properties of air


along with total pressure, one then can find the
rest of the properties

h
W

Ws
f

For example

Ws
W

Given tdry, twet, air pressure pa

Find h, RH, dew point temp.


humidity ratio etc

Excel for Engineers-part2

t.
sa

dry temp.

14

humidity ratio

3-Moist air properties

55

td

Excel for Engineers-part2

56

3-Moist air properties

Some of the moist air properties relation

W = 0.622 Pv
P - Pv
*

W=

Ws ( 2501 - 2.326 t * ) - (t - t * )
(2501 + 1.86 t - 4.186 t * )
Pvs ( t d ) =

WP
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

15

59

Excel for Engineers-part2

60

4-Water thermodynamic properties

4-Water thermodynamic properties


T

CP

Steam and water thermodynamic and


thermo-physical properties

P2 > P1
P1

Sub-cooled region

Sub-cooled region

Saturated region

Saturation region

Super heated region

Saturated vapor line

Superheated region

Saturated liquid line


s
Excel for Engineers-part2

61

4-Water thermodynamic properties

Excel for Engineers-part2

16

Excel for Engineers-part2

62

4-Water thermodynamic properties

63

Excel for Engineers-part2

64

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

Superheated steam properties given P & T

Given the temperature T ( in deg. C)


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

17

67

Excel for Engineers-part2

68

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

Insertion of a form

Excel for Engineers-part2

Insert controls on the userform

ComboBox

Command
button

Forms, user
form1

Tools

Label
Toolbox for
Controls
Text Box

Properties window
Userform

Excel for Engineers-part2

18

70

71

Properties
window

Userform
frame

Excel for Engineers-part2

72

Example: Simple Program


Construct a program with user interface
to add two numbers and show the result

19

Excel for Engineers-part2

73

Excel for Engineers-part2

74

Excel for Engineers-part2

75

Excel for Engineers-part2

76

Excel for Engineers-part2

77

Excel for Engineers-part2

78

Summary
Conclusions & Recommendations

1-Cell Referencing (Relative & absolute)


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


capability of using Excel for your courses

5-Forms toolbox

4-Learn how to use new functions, build your


own functions and form modules

6-User defined functions


7-Usful Engineering Applications

5-For full excel usefulness learn VBA

8-Userform and VBA


Excel for Engineers-part2

20

79

Excel for Engineers-part2

80

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

21

83

Excel for Engineers-part2

84

Potrebbero piacerti anche