21
1 Excel for Engineers-part2 1 Excel for Engineers-part2 2 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 3 1-Macros in Excel Excel for Engineers-part2 4 How to record a macro 1-Go to toolsmacrorecord 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) 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 Macros in Excel

excel_for_eng_02.pdf

Embed Size (px)

Citation preview

1

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

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 3

1-Macros in Excel

Excel for Engineers-part2 4

How to record a macro

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

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

Macros in Excel

2

Excel for Engineers-part2 5

Macros in ExcelRecord a macro

Excel for Engineers-part2 6

Record a macro Macros in Excel

Excel for Engineers-part2 7

Macros in ExcelRecord a macro

Excel for Engineers-part2 8

Macros in ExcelRecord a macro

3

Excel for Engineers-part2 9

Record another macro Macros in Excel

Excel for Engineers-part2 10

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

Macros in ExcelAssign a macro to a command button

Excel for Engineers-part2 11

Assign a macro to a command button Macros in Excel

Excel for Engineers-part2 12

Assign a macro to a command button Macros in Excel

4

Excel for Engineers-part2 13

Assign a macro to a command button Macros in Excel

Excel for Engineers-part2 14

Macros in ExcelAssign a macro to a command button

Excel for Engineers-part2 15

Form controls

You can create scroll bar, list_box, combo_box and others on Excel sheet using the form controls

Excel for Engineers-part2 16

Form controls

5

Excel for Engineers-part2 17

Form controls

Excel for Engineers-part2 18

Command button

Group box

Combo Box

List Box

Scroll Bar

Check Box

Option button

Form controls

Excel for Engineers-part2 19

Form controls

Excel for Engineers-part2 20

Form controls

6

Excel for Engineers-part2 21

Form controls

Excel for Engineers-part2 22

msgBox and inputBox

Excel for Engineers-part2 23

InputBox Function

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)

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

Syntax

Excel for Engineers-part2 24

InputBox Function

7

Excel for Engineers-part2 25

Input Box

Excel for Engineers-part2 26

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

Massage Box

Syntax

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

User Defined Functions

8

Excel for Engineers-part2 29

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

Creating user defined functions

Excel for Engineers-part2 30

User defined functions

Excel for Engineers-part2 31

Suppose we want to add a function that do the followings

2210 xaxaay ++=

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

User defined functions

Excel for Engineers-part2 32

User defined functions

9

Excel for Engineers-part2 33

Select a cell and type

=myfun(1)

User defined functions

Excel for Engineers-part2 34

User defined functions

Excel for Engineers-part2 35

To see all user defined functions and subroutines

go to tools→macro→visual basic editor

And press on module

Excel for Engineers-part2 36

Notes on creating user defined functions

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

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.

10

Excel for Engineers-part2 37

Another Example of creating A user defined functions

The effectiveness for counter current flow heat exchanger

Excel for Engineers-part2 38

Example on creating functions

Excel for Engineers-part2 39 Excel for Engineers-part2 40

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 effectivenessIf Cr = 1 ThenCr = 0.999End Ifx1 = 1 - Exp(-NTU * (1 - Cr))y1 = 1 - Cr * Exp(-NTU * (1 - Cr))eff_counter = x1 / y1End Function

List of the function eff_counter

11

Excel for Engineers-part2 41

Modules can be saved and insert in new worksheet

Steps to export a module

Go to visual basic editor

Choose the module to export

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 42

Steps to insert a module into a worksheet

1-In a new or current workbook go to Visual Basic editior (Alt-F11)

2-Insert a new module

3-Click on the module with mouse right click select import file

4-A pop up window will appear to select the module you want to insert

5-Choose the location and the name of the file (module) you want to insert

Excel for Engineers-part2 43

Inserting a module into excel sheet

Excel for Engineers-part2 44

Some Useful Applications

1-Table interpolation

2-Friction factor in pipes

3-Moist air properties

4-Thermodynamic properties of water

12

Excel for Engineers-part2 45

1- Table interpolation

Program name:

table interpolation.xls

Excel for Engineers-part2 46

1- Table interpolation

Excel for Engineers-part2 47

1- Table interpolation

Excel for Engineers-part2 48

1- Table interpolation

13

Excel for Engineers-part2 49

Cp_air(T)

Prandtl_air(T)

Density_air(T)

Conductivity_air(T)

Air properties

Water propertiesCp_water(T)

Prandtl_water(T)

Spvolliq_water(T)

Conductivity_water(T)

1- Table interpolation

Excel for Engineers-part2 50

2- Friction factor for flow inside pipes

Program name f_factor.xls

Excel for Engineers-part2 51

⎥⎦

⎤⎢⎣

⎡+−= 5.05.0 Re

51.27.3

/log0.21f

Def

Re64

=fLaminar flow

2- Friction factor for flow inside pipes

Re<2300

Turbulent flow Re>=2300

Excel for Engineers-part2 52

2- Friction factor for flow inside pipes

14

Excel for Engineers-part2 53

2- Friction factor for flow inside pipes

Excel for Engineers-part2 54

3-Moist air properties

Dry bulb temp

Wet bulb temperature

Total pressure

Humidity ratio

Enthalpy

Relative humidity

Dew point temperature

Excel for Engineers-part2 55

Given tdry, twet, air pressure pa

Find h, RH, dew point temp. humidity ratio etc

3-Moist air properties

Given two thermodynamic properties of air along with total pressure, one then can find the rest of the properties

For example

Excel for Engineers-part2 56

hW

dry temp.

hu

mid

ity r

atio

Ws

W

tt d

h

sat. t *

1

Ws*

f

3-Moist air properties

15

Excel for Engineers-part2 57

3-Moist air properties

Excel for Engineers-part2 58

)t 4.186 - t 1.8 + (2501t- (t - )t 2.3 - 2501 (W

= W s*

***

6) 26

W+0.622P W = )t(P dvs

P-PP0.622 = W

v

v

) t* 1.8 + (2501 W+ t = ) t* C+ (2501 W +t C = h pvpa 6*

(T) C + T C + T C + T C + T C + C + /TC = )P( 74

63

52

4321vs lnln

vs

v

PP = φ

Some of the moist air properties relation

Excel for Engineers-part2 59

3-Moist air properties

Excel for Engineers-part2 60

4-Water thermodynamic properties

File name: Xsteam_excel_v2.6.xls

Site: http://www.x-eng.com

16

Excel for Engineers-part2 61

Steam and water thermodynamic and thermo-physical properties

Sub-cooled region

Saturation region

Superheated region

4-Water thermodynamic properties

Excel for Engineers-part2 62

P

P

T

CP

1

2 > P1

Saturated liquid line

Saturated vapor line

Saturated region Super heated region

Sub-cooled region

s

4-Water thermodynamic properties

Excel for Engineers-part2 63

4-Water thermodynamic properties

Excel for Engineers-part2 64

4-Water thermodynamic properties

17

Excel for Engineers-part2 65

4-Water thermodynamic properties

Excel for Engineers-part2 66

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

4-Water thermodynamic properties

Excel for Engineers-part2 67

Given the temperature T ( in deg. C)

Saturated pressure (Psat) : Psat_p(T)

Saturated liquid enthalpy hf: hl_t(T)

Saturated vapor enthalpy hg: hv_t(T)

Saturated liquid specific volume vf: vl_t(T)

Saturated vapor specific volume vg: vv_t(T)

4-Water thermodynamic properties

Excel for Engineers-part2 68

Superheated steam properties given P & T

1-Enthalpy h: h_pt(P,T)

2-Entropy s: s_pt(P,T)

3-Density rho: rho_pt(P,T)

4-Water thermodynamic properties

18

Excel for Engineers-part2 69

4-Water thermodynamic properties

Excel for Engineers-part2 70

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 71

Userform

Forms, user form1

Toolbox for Controls

Insertion of a form

Properties window

Excel for Engineers-part2 72

Userform

Tools

Properties window

Command button

ComboBox

frame

Text Box

Label

Insert controls on the userform

19

Excel for Engineers-part2 73

Example: Simple Program

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

Excel for Engineers-part2 74

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

20

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

Excel for Engineers-part2 79

Summary

1-Cell Referencing (Relative & absolute)

2-Introducing some of the built in functions & Extend your use of Excel

3-Use of Goal & Seek and Solver

4-Macros in Excel

5-Forms toolbox

6-User defined functions

7-Usful Engineering Applications

8-Userform and VBA

Excel for Engineers-part2 80

Conclusions & Recommendations

1-Excel is handy, available and powerful

2-Lots of help and examples from the net

3-It is highly recommended to extend you capability of using Excel for your courses

4-Learn how to use new functions, build your own functions and form modules

5-For full excel usefulness learn VBA

21

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

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

References for Excel uses and VBA programming

1-Excel for Engineers

2-Internet (lots of sites, examples and forums