Sie sind auf Seite 1von 3

Creating a Function in Excel

Introduction

Formulas are very important in solving chemical engineering problems. However, some of these formulas
are too long and are weary-some to input into a computer program repeatedly. This lecture will guide the
students on how to input long formulas in excel as a function. It also includes topics on how to determine
equations from tabular data.

Creating a Function

To create a function in excel,

1. Open VBA by pressing ALT + F11


2. Right click the white portion of the VBA
3. Select “Insert Menu”
4. Click “Module”. After clicking the module, a working
space in white background will appear
5. The syntax in creating a function is as follows

Function “name of the formula”(“required input


variable”)
6. Type the syntax shown in step 5 with the desired
formula name and required input variable
7. Click enter
8. Type the equation of the formula

Figure 1: Creating a Module in Excel

As an example consider the formula for determining the vapor pressure of liquid water as a function of
temperature taken from the Handbook (Section 12 page 12-5),
−6096.9385
ln(𝑝𝑠 ) = + 21.2409642 − (2.711193 𝑥 10−2 𝑇) + (1.673952 𝑥 10−5 𝑇 2 ) + 2.433502ln(𝑇)
𝑇

where ps is in Pascal and T in Kelvin. Following steps1 to 8, the output is shown in figure 2. Note that “ps” is
the function name and “T_K” is the required input variable. Choosing how to represent the “function name”
and the “required variable” is discretion of the student. You should choose representation in a way that you
can easily recall the function and what is needed by the function. For example, I choose “T_K” as the
required input because it will remind me that the required input is temperature “T” and it should be in Kelvin
“K”. It is also notable that the equivalent of “ln” in VBA is “log”. Another way of writing the above equation is
shown in figure 3.

Figure 2: Function Syntax

Figure 3: Alternative way of representing the formula

On figure 3, it can be observed that the input variable is in Celsius “C”. Since the formula requires
temperature in Kelvin, the input “T_C” must be converted to Kelvin. Conversion is shown in line 3. Note
also of the color of the lines. Blue colored-lines denote the main operation you are going to perform, either
“Sub routine” or “Function”. Black colored-lines are the executable codes while the green colored-lines are
comments. Comments will be ignored during the execution of the code. It is good to integrate comments
into a code because it will help you recall what are those lines for.

How are you going to use the function? Well, just go to your spreadsheet and click the cell you want to
input your function. Input “=” then the name of the function and the required input value, press enter.
Example on how to use the vapor pressure function is shown in the figure 4 and 5.

Figure 4: Inputting the Function


Figure 5: Result of the Function

Saving a Function for Future Use

To permanently saved a function, saved the Excel file as an “Excel Macro-Enabled Workbook” as shown in
figure 6.

Figure 6: Saving a Workbook with Function

Classroom Activity

1. Create a function for the density of water as a function of temperature


2. Develop a function for the determination of latent heat of vaporization of water as a function of
temperature
3. Create a function that is capable of determining the saturation temperature if the saturation
pressure is given

Das könnte Ihnen auch gefallen