Академический Документы
Профессиональный Документы
Культура Документы
E
ngineers have relied on tools such as cardboard duct calculators, cel that provide a comprehensive review
of Visual Basic for Applications. You
log-log graphs and lengthy tables for information that was difficult to cal- will need to consult these references for
a complete understanding of the subject.
culate directly. With the spreadsheets that are available today, calculations can
Calculations
be made easily and placed into user-friendly functions. Building on the method Before writing the UDF procedure
for duct diameter, the calculations can
used in “Solving for Friction Factor,”1 this article presents user defined functions be done manually in a standard spread-
sheet. Here, it can more easily be tested,
(UDFs) that can be used as an alternative to the duct calculator. debugged and verified for accuracy. For
someone not familiar with the creation
The duct calculator typically is used The duct calculator also can be used process for UDFs, it is also easier to see
to find the duct diameter given flow to find the flow given the duct diameter what calculations are being executed.
and design pressure drop. When the and design pressure drop or find the pres- The spreadsheet procedure, under the
cardboard duct calculator was created, sure drop given flow and duct diameter. “Dia Air” tab, is then replicated as a
assumptions were made for the specific It also provides a graphical solution for UDF.
volume, viscosity and absolute roughness rectangular equivalents to the diameter. The procedure used to create these
of the duct material. These assumptions All of these functions are built into an functions is a form of iterative problem
can lead to significant error, particularly Microsoft Excel spreadsheet that can solving. In some cases, a solution of the
with the absolute roughness that varies be downloaded from www.ashrae.org/ Colebrook Equation is required. The
significantly between the duct materials ashraejournal/Lester_Duct_Calculator_
that commonly are used. The approach UDFs.xls. This article provides a brief About the Author
in this article uses actual values for these explanation of how the spreadsheet and Thomas G. Lester, P.E., is a retired senior me-
properties. UDFs were created, what user defined chanical engineer in Rochester, N.Y.
Diameter
Diameter
Projected
Diameter
for the second line of the pair.
High
Low
This spreadsheet was presented to show the calculation but is
good for only one point at a time. The calculation has been written
into a UDF named “AirDia,” which makes the procedure easy to Figure 2: In Step 5 the projected diameter is calculated using simple
use in a network of ducts with multiple points. The next section triangulation.
gives a brief explanation of UDFs, and how they can be used.
types of VBA macros: command macros (sometimes referred
Visual Basic for Applications (VBA) and Excel as keystroke macros) and user defined functions (sometimes
D54214asjl_805523
Visual 4/9/08 (VBA)
Basic for Applications 5:15 AM Page 1 in Excel.
is included referred to as function procedures). It is the user defined func-
Among the uses of VBA is the creation of macros. There are two tions that I will focus on.
CRITICAL CLEANING
DEMANDS GOODWAY.
www.info.hotims.com/25207-18 www.info.hotims.com/25207-19
44 A S H R A E J o u r n a l August 2009
Access to VBA has changed in Excel 2007. Under the “Of- Dim Darcy As Single
fice Button,” click on “Excel Options.” Under the “Popular
Tab,” “Top Options for Working with Excel,” check the Vel = CFM / (3.14159 * (Dia / 12) ^ 2 / 4)
“Show Developer Tab in the Ribbon.” Select the “Devel- RelRough = AbsRough * 12 / Dia
oper” tab and Visual Basic will appear in the left side of ReynoldsNum = Vel / 60 * (Dia / 12) / KineVisc
the options. In Excel 2003 and older versions, select Tools FF = fCalc(RelRough, ReynoldsNum)
> Macros >Visual Basic Editor. Either path takes you to the Darcy = (Vel / 60) ^ 2 / (2 * 32.174) * FF * 100 * 12 / Dia /
Visual Basic Editor. UDFs are created and/or edited in the (62.4 * SpVol) * 12
Modules section.
AirPD = Darcy
Viewing a User Defined Function
You do not need to view a UDF to use it. This section is provided End Function
for anyone wishing to look at the code used in creating the UDFs.
While UDFs follow the general format of Visual Basic pro- The first line uses the “Function” keyword, identifying the
gramming, they don’t necessarily contain any special program- procedure as a UDF. “AirPD” is the function name, followed
ming language. The sample spreadsheet includes five UDFs; by the “arguments” inside parentheses. These “arguments” are
the simplest is the procedure for calculating the pressure drop values to be used in the calculations.
in an airstream, “AirPD,” shown below: The “Dim Statements” are then used to establish variables
and the precision to be used. The “As Single” will apply a
Function AirPD(CFM, Dia, AbsRough, KineVisc, SpVol) single-precision floating point to a variable.
Calculation statements for velocity, relative roughness and
Dim Vel As Single Reynolds number follow. The calculation of friction factor (FF)
Dim RelRough As Single uses the procedure fCalc (Module11) that was introduced in
Dim ReynoldsNum As Single “Solving for Friction Factor.” The last calculation is the Darcy-
Dim FF As Single Weisbach formula with conversions to units of inches of water.
ELECTRONIC FLOWHOOD
FLOWHOOD
DIRECT DIGITAL READOUT 25-2500 CFM
www.info.hotims.com/25207-37 www.info.hotims.com/25207-29
August 2009 ASHRAE Journal 45
The statement “AirPD = Darcy” sets
the name of the UDF equal to the result-
ing calculation. The “End Function” key-
words indicate the end of the procedure.
In this case, all the statements are simple
mathematical computations. In fact, I use
only one Visual Basic procedure in any of
the UDFs presented: a “Do Loop Until.” I
use this to replicate the tables and also to Figure 3: Slope and projection triangles.
execute the slope and projection calcula-
tions. The calculations are identical to the spreadsheet. In the 3. The function dialog box appears, and then select “User
tables, I stop the incrementing as soon as a positive value is calcu- Defined” from the categories dropdown menu.
lated for the design pressure drop minus the actual pressure drop. 4. From the list of available functions, select “AirDia.”
5. Another dialog box opens with the “arguments” listed.
Using a Recorded UDF Enter relative references for flow and absolute rough-
There are five UDFs in the sample spreadsheet. ness and absolute references (using the F4 key after the
1. fCalc (RelRough, ReynoldsNum) (Module11). This UDF cell reference is entered) for the global inputs for design
solves for friction factor given relative roughness and static, kinematic viscosity and specific volume. (Global
Reynolds number. Note: fCalc must be imported to make references will apply to all duct sections.)
the other functions work properly because it is embedded 6. Once the “arguments” have been added, the results should
in the other functions. appear in the dialog box; click “ok” and the UDF will be
2. AirDia (CFM, DesignStatic, AbsRough, KineVisc, completed in Cell I22.
SpVol) (Module2). This UDF solves for duct diameter 7. The UDF can be copied to the range I23:I26.
given the arguments cfm, design static, absolute rough- 8. If the “arguments” have been entered correctly, I22:I26
ness, kinematic viscosity, and specific volume. should match D22:D26.
3. AirCap (Dia, DesignStatic, AbsRough, KineVisc, SpVol) For use in an existing spreadsheet, an Excel add-in can be
(Module1). This UDF solves for the flow (CFM) given created by saving the sample spreadsheet in .xla format. This
the stated arguments. is accomplished by performing a “Save As” and specifying
4. AirPD (CFM, Dia, AbsRough, KineVisc, SpVol) (Module5). “.xla” format. The saved add-in can be used as any other
This UDF solves for pressure drop given the stated arguments. Excel add-in.
5. WCalc (Height, EquivDia) (Module3). This UDF solves Alternatively, the modules could be exported from the sample
for the Huebscher equivalents for rectangular duct.2 spreadsheet and imported into an existing spreadsheet. While
When opening a spreadsheet that contains macros, Excel asks this requires some familiarity with the Visual Basic Editor, it
to “Enable Macros.” For the UDFs to execute, you must “Enable is relatively easy to export and import the modules. Within
Macros.” In Excel 2003 and older versions, a dialog box appears the editor, under the “File” menu, you will see the commands
when opening a spreadsheet with macros. This is less obvious in “Export File” and “Import File.” Highlight a module and then
Excel 2007, where only a “Security Warning” appears below the export it as a “.bas” file. The module can be imported into an
“ribbon” and an “Options” button appears. Click on “Options” existing spreadsheet. When all the modules have been added,
and select, “Enable this content.” Without “Enable Macros,” the save the spreadsheet and close it. Re-open the spreadsheet, “En-
spreadsheet will not perform any of the calculations described. able Macros,” and they will appear under the “User Defined”
UDFs work the same as the hundreds of built-in functions exist- category in the functions dialog box.
ing in Excel. Available functions are listed by categories. UDFs are
placed in a category named “User Defined,” which is automatically Conclusions
created whenever UDFs are created. In this category, all the UDFs Engineering tools such as the cardboard duct calculator per-
in “Add-ins,” as well as those created in the spreadsheet, are shown. form functions that easily can be performed directly in a spread-
If a new spreadsheet is created, the sample spreadsheet can sheet using user defined functions. By using the actual values
be used by deleting all of the worksheets that are not desired. for properties of air and duct material, it is possible to improve
When this is saved, all of the UDFs will be saved with it. the accuracy of the results. With practice and familiarity, these
The sample spreadsheet contains examples of the use of each calculations can be performed more efficiently using UDFs.
of these functions. Under the “Examples” tab, the procedure to
enter a UDF would be: References
1. Place the cursor in Cell I22 (or any blank cell). 1. Lester, T. 2003. “Solving for Friction Factor.” ASHRAE Journal
2. In Excel 2007, select the “Formulas” tab and then select 45(7):41 – 44.
“Insert Function” or from Excel 2003 or older, just click 2. 2005 ASHRAE Handbook—Fundamentals, Chapter 35, Duct
on the “fx” icon. Design, p. 35.9.