You are on page 1of 13

Mathcad and Visual Basic for

Applications



As evaluated by Terrence Stevenson
3/14/2014
Introduction
As an engineer I am frequently confronted with problems which can take a long time to solve because,
though the process may be simple the problem may require that process to be repeated many times.
This is the way an integral works, the integral itself is usually quite simple and is quickly and easily solved
by integrating, but if you had to do that process manually much more time would be required. In this
paper I will consider some aspects which make each program a powerful tool and some things that
make each program very frustrating. I am testing these programs for me because I have not used either
one for programming in a long time and I have a project coming up which will require me to choose one.
Audience
My audience for this evaluation is anyone looking for a way to program mathematical functions. A
familiarity with either of these programs is not required but I think many people are in the same
situation as me right now, I have programmed with both Mathcad and VBA but every time the need
arises I still debate which one I should use. I would like to use this evaluation to show some strengths
and weaknesses of each program which will help me and my audience. I will attempt to refrain from
being too confusing so that someone who may not be familiar with programming can follow along but
these programs are not very useful outside of mathematics or engineering in the context of my
evaluation.
Methods
The major difference between VBA and Mathcad is that VBA contains all the programming code but is
hidden behind Excel. This makes it simple for someone unfamiliar with programming to utilize a
program written by someone else. In order to really test these two tools I wrote a program in both
Mathcad and VBA relating to what we are currently studying in my Geotechnical Engineering class.
Finding common ground is a good way to compare tools like this because both of these tools are
extremely powerful and this allows for ease of use and personal preference to be fairly judged. I am
going to test each program against the following tests:
1. Start a new program
2. Use of equations
3. Call out variables
4. Output variables
5. Create nested loops within the program
6. Make the program user-friendly
7. Make changes within the program
8. Debug the program
9. Units
10. Risk of crashing

Results
Task 1: Start a new program
Mathcad Starting a new program in Mathcad is very simple and straightforward. All you do is double-
click on the executable program file and Mathcad will open with a blank page waiting for you to begin
typing.
VBA In order to start a new program in VBA you must first open excel. If there is no developer tab at
the top of the page it is necessary to install the VBA add-in. After this is done and the developer tab has
shown up, you must click on it and then click on the Visual Basic link located on the far left of the excel
links. Doing this will open a new window; here you must click on the Insert tab, then module. This will
open a new module for you to start programming.
For this task Mathcad is much simpler. This is because Mathcad is a very visual program; it displays your
equations just as you would write them on paper so it is easy to follow along. VBA is not meant to be
seen by the user, it is meant to be running in the background in Excel.
Task 2: Use of equations
Mathcad As stated above, Mathcad displays equation just as you would write them on paper. The only
tricky part about this is how you type the equal sign. There are four different equal signs in Mathcad and
they each define the variable in a different way. The equal sign needed for typing an equation is
displayed by typing the colon.
VBA There are no trick to typing an equation in VBA, you type it the same as you would in Excel. The
only problem here is when your equation gets to be longer it is difficult to see if it is calculating
correctly. The equation is not displayed in the same user-friendly way that Mathcad does.
For this task I like how Mathcad displays the equations much better. The different equal signs might
seem slightly intimidating but it is logical and therefore not an impediment to the user, but it is
something the user must remain aware of.

Task 3: Call out variables
Mathcad To call out a variable within a program in Mathcad it is necessary to open the programing
tool palette. In this palette there is an arrow pointing to the left, this is used as an equal sign to set a
value or equation equal to a variable. Programming tool palette shown in Figure 1.

Figure 1
VBA Here you only need to set your desired variable equal to your value or equation. Once that
variable is defined VBA will know what it is everywhere else in that program. If the user desires a
variable from Excel to be called out it is necessary to define that variable and relate it to the specific cell
in Excel, this allows VBA to call directly from Excel in real-time. This is shown in Figure 2.

Figure 2
VBA is easier to use for this task because it is not any different than defining a variable elsewhere in the
program. In Mathcad the user must constantly remember that the programming tool palette must be
used in order for the program to function. It can be very time consuming if the user requires many
variables to be called out from Excel, although it is very convenient to be able to easily change values in
Excel and still have the program function properly without having to open VBA.


Task 4: Output Variables
Mathcad To output a variable from a program in Mathcad the user must call out the program name
and just press the equal sign. This is very simple and convenient. Figure 3 shows the entire program and
how it outputs your specified variable.

Figure 3
VBA In order to output a variable using VBA it is necessary to give VBA a cell reference in Excel to
output that variable to. This is very similar to calling out a variable in VBA from Excel and is not
complicated, but once more, if you have several variables you wish to output to Excel it can be time
consuming. Figure 4 shows how this is done.

Figure 4
For this task Mathcad is much nicer because you can immediately tell if the program is functioning
properly. You cannot output variables into VBA; you must export them to Excel to see if everything is
working as desired.

Task 5: Create nested loops within the program
Mathcad This can be complicated because everything must be done in exactly the right format or else
the program will not loop correctly. This can be quite frustrating because unless you already know what
the answer should be you might not know if the program is running incorrectly. Figure 5 shows the
proper way to format nested loops.

Figure 5
VBA Nesting loops in VBA is very simple, just dont forget to call your next j before you call your
next i otherwise it will loop incorrectly. Figure 6 shows how this is done, I tab the nested part over to
make it easier to differentiate the loops but it is not necessary for the program to function.

Figure 6
This is much easier to do in VBA, the specific formatting required by Mathcad can become very annoying
and in my experience multiple attempts are required before the program will function as desired.

Task 6: Make the program user-friendly
Mathcad If the program is functioning properly it is not difficult for another user to come in and
change variables to meet the desired parameters. If the person who wrote the program spent time to
label and define variables clearly then this should not be confusing. The second person must be careful
not to change anything within the program or else everything will turn red and debugging is required.
VBA If the person writing the program spends the time it is possible to make programs in VBA very
simple to use through Excel. In the program I wrote I have message boxes that pop up to walk the user
through the entire process. This is initiated by pushing the button labeled begin. Figures 8 through 16
show the text boxes that pop up which prompt the user to respond. Figure 7 shows how I create the text
boxes.

Figure 7

Figure 8

Figure 9

Figure 10

Figure 11

Figure 12

Figure 13

Figure 14

Figure 15

Figure 16
The winner of this task might vary user to user but in my opinion VBA is much more user-friendly for
anyone to use. Mathcad is nice because the user can see what is happening to generate the answer; this
is much more difficult to see in VBA.

Task 7: Make changes within the program
Mathcad Making changes within a Mathcad program can be very frustrating, I have often found myself
starting over from scratch because the format within the program is not allowing me to do what I want.
While writing this program I rewrote it several times while trying to get my nested loop to function
properly. This is mostly because I did not remember the proper formatting and it became very
frustrating. I had to use Google to search for examples. This is further complicated by Mathcad running
the program every time a change is made. If something is formatted incorrectly the program will turn
red and debugging is required. If a loop is poorly defined Mathcad could try to run the loop infinite time
resulting in a crash. In this instance hopefully you saved recently, otherwise you will be starting from
scratch.
VBA VBA makes it easy to make changes as it is not as particular about formatting. Just as in Mathcad
it is possible to poorly define a loop and have VBA crash on you but it is not as big of a deal because
Excel will recover the program and since VBA does not run the program until you tell it to you can make
the appropriate changes without the risk of crashing again.
VBA is the very clear winner here. Mathcad makes this process so complicated that users can become
quickly frustrated.

Task 8: Debug the program
Mathcad There is a debugging function in Mathcad but it does not work very well, I have never
personally had it actually help me debug anything. The way that I approach a program in Mathcad is to
start small so that I can ensure everything is working. With this approach if I put something in that
Mathcad does not like I can easily find the issue. The problem with this approach is that I am constantly
required to make changes to the program, which I discussed above. Figure 17 shows Mathcad indicating
a problem.

Figure 17
VBA VBA has a very nice debugging program; it runs through the program line by line and stops on the
line with the problem. It even does a very good job of telling you where the problem is within that line.
Figure 18 shows how VBA walks through the program with the debugging feature.

Figure 18
Debugging is performed much better by VBA than by Mathcad, the debugging function is very smart and
navigates the code very logically to find the problem. Debugging in Mathcad can be extremely
frustrating and time consuming.

Task 9: Units
Mathcad The use of units makes Mathcad a very powerful tool. I struggle to make them work in a
program but in the normal use of Mathcad this is a nice way to debug equations because you know what
units a variable should have. Figure 19 shows the use of units in Mathcad, if I were to use these variables
in an equation it would output a solution with the correctly labeled units. This also makes it easy to
convert between U.S. units and SI units since Mathcad does it for you.

Figure 19
VBA VBA does not use units, it is the responsibility of the user to keep track of them and to make sure
the equations are written properly. In Figure 20 I had to manually type these units in the adjacent cells.

Figure 20
I really like being able to use units in Mathcad, even though they can complicate programs. It is easy to
turn units off if you do not want to use them.

Task 10: Risk of crashing
Mathcad I discussed this a few times above but in overview the risk of Mathcad crashing can be very
high, especially depending on the speed of your computer. If Mathcad crashes on you it will not recover
your work, you have to start over from your last save. Needless to say, this can be extremely frustrating.
VBA There is not a high risk of crashing with VBA, though I have had it happen a few times when I had
a poorly defined loop. Excel is smart though and will recover your work for you so even if VBA does
crash it is nothing but the slight inconvenience of having to restart Excel.
Recommendations
After performing all these tests and writing identical programs in both Mathcad and VBA I have decided
that my next program will be written in VBA. VBA does require a bit more work to call out and output
variables but in the end it is much easier to make changes within the program and to debug any errors.
My recommendation would be to try both programs and play around with them to learn their respective
features as they are both very powerful and useful tools with strengths and weaknesses which make
each desirable for different purposes.

You might also like