Part 2: Excel VBA for Engineers-Rebar Custom Functions


If you haven’t done so see Part 1: Excel VBA for Engineers for an introduction on VBA and setting up the VBA development environment.


Below are the VBA custom functions I’ve develop. This is repeated here with modifications from Part 1: Excel VBA for Engineers for your convenience. See Figures 1-3 and the embedded video for examples of how these custom rebar functions can be utilized in Excel.

Figure 1 – Rebar Custom Functions
Figure 2 – Cell Reference
Figure 3 – String Parameter
Video 1 – Rebar Custom Functions


=RebarArea(value) – returns area (in2)

=RebarDiameter(value) – returns the diameter (in)

=RebarWeight(value) – returns the weight (lb/ft)

The RebarArea, RebarDiameter, and RebarWeight syntax has the following argument:

value – Rebar id. This is a string (i.e. #4, #5…….#18) or a reference to a cell holding a string.





Now will dive into the VBA code. For simplicity I created one module for this blog post. You can download the module here: VBA_Part2_Blog.bas. For production I typically move functions such as IsArrayAllocated(Arr As Variant) As Boolean into a “utility” module because of its applicability on other projects.

Figure 4 shows the Declarations for the module. Rebar data is stored in an array of arrays called RebarTable. These array types are also called jagged arrays. Each rebar array (i.e. BarNo3, BarNo4, and etc.) holds the rebar data – area, diameter, and weight. We’ll have more on this later. Note that these arrays hold Variants. A Variant in VBA is a variable datatype that can be any datatype. For more information on Variants see this reference. The BarNo arrays could have been declared to hold Doubles instead of Variants, but I came across a conflict when attempting to use the rebar custom functions with Combo Boxes.

Note that all variables in Figure 4 are declared as Private except for the array RebarTable which is Public. Public variables are available in other modules. I use RebarTable for design functions and subroutines that are defined in other modules. For more information on variable declarations see this reference.

Figure 4 – Declarations

Figure 5 shows one of the three Custom Rebar Functions. The other functions are coded similarly. The RebarArea function is declared Public so it will be available in the Excel Worksheets. See Figure 1 for an example. This function passes RebarId which is a String and returns the area of the bar as a Double. Figure 3 shows the call. The function can be called with a cell reference as shown in Figure 2. If the cell doesn’t hold a valid String (#4, #11, etc.) the return value will be 0. My production code returns an Error, but this feature is beyond the scope of this blog post.

You will notice at the beginning of the function I call IsArrayAllocated. This function checks to see if the RebarTable has been initialized with the rebar data. See Figure 5 for the code. If the RebarTable hasn’t been initialized I call the function InitializeRebarTable which is shown in Figure 6. I use Select Case to match the RebarId with the correct RebarTable array indexes. For more information on Select Case see this referernce. Here you see why I defined the Id and Prop Enums shown in Figure 4. For more information on Enums see this reference. Array calls with integer indexes can be error prone as shown below:

RebarTable(Id.No3)(Prop.Area) is equivalent to RebarTable(0,1)

Figure 5 – RebarArea Function

The InitializeRebarTable subroutine populates the BarNo arrays and RebarTable. For more information on Arrays see this reference. One thing of note in this subroutine is the line- continuation underscore character ( _ ). The underscore proceed by a space allows lines of code to broken into multiple lines.

Figure 6 – RebarTable Initialization

The IsArrayAllocated function was found at this website. This website is one of the better Excel VBA references.

Figure 7 – Array Allocation Check

Part 1: Excel VBA For Engineers


What is VBA?

Visual Basic for Applications (VBA) is the standard programming language for Microsoft Office products including Excel. VBA allows the user to automate Excel tasks by writing macros, subroutines, and functions

Why use VBA?

  • Automate repetitive tasks
  • Simplify complex Excel equations with Basic language code
  • Reduce spreadsheet errors
  • Make spreadsheets more maintainable
  • Automate tasks in software programs such as SAP2000 or AutoCAD

Note: This post uses Excel 2013 but the concepts will apply to any recent version of Excel.

Do you use the built-in Excel LOOKUP functions? Do you use these functions to extract reinforcement data (area, diameter, weight, and etc.) from a range of cells? Yuck! I can never remember the syntax for the LOOKUP functions. Over the years I have made many errors with these convoluted functions. Not only are they error prone, but creating a range of reinforcement data is another possible source of errors.

Here are the functions that bridgeautomation uses to enter reinforcement data into Excel replacing the error prone LOOKUP functions.


RebarArea, RebarDiameter, and RebarWeight returns the area, diameter, and weight in Imperial units respectively. The base units are inch and lb.





The RebarArea, RebarDiameter, and RebarWeight syntax has the following argument:

value – Rebar id. This is a string (i.e. #4, #5…….#18). This parameter must be enclosed in quotation marks (i.e. RebarArea(“#4”).

Start typing “Rebar” in a cell and a drop down box opens making it easy to remember the function name.

No LOOKUP tables or typing in reinforcement data into cells!

VBA Setup

Verify that the DEVELOPER menu item is visible.

If the DEVELOPER menu item is not visible navigate to the FILE / OPTIONS dialog box.

At the Options dialog box select Customize Ribbon and check the Developer checkbox and then press the OK button.

Hello World Example

Create a new Excel worksheet and save as an Excel Macro-Enabled Workbook (*.xlsm). From the main menu select the Developer menu item and then click on Visual Basic.

Select Insert and Module.

In the Project Tree in the left pane of the dialog box below click on Module 1. Enter the following code as shown below. You can either type the code in or copy and paste from code textbox below.

Source code for the HelloWorld function is below.

Public Function HelloWorld(flag As String)
     If flag = 0 Then
         MsgBox ("Hello World From Bridgeautomation")
         HelloWorld = "0 passed-Hello From Bridgeautomation"
         HelloWorld = flag + " passed-Hello World From Bridgeautomation"
     End If
 End Function


HelloWorld is a simple example function that shows how to pass parameters, return values, and use message boxes.

  • A 0 value parameter opens a message box and returns a string message to the cell
  • Any other numeric value parameter just returns a string message to the cell
  • Non-numeric value parameter returns a #Value! error



The HelloWorld function syntax has the following argument:

value – Any numeric value.

The graphic below shows how to use the HelloWorld function. In the example below cell C3 is referenced for input. Entering the integer 100 as a parameter would have worked as well (=helloworld(100)).

The graphic below shows the final results in cell C5 for an input value of 3 in cell C3.

The Excel file can be found here: Example HelloWorld