Wednesday, 11 April 2018

An introduction to Visual Basic for Application (VBA)


To understand Visual Basic for Application, let's look at the following questions.

What is Visual Basic for Application?

Visual Basic for Applications also known as VBA is a programming language developed by Microsoft. SOLIDWORKS, along with Excel and other software Office Suit, includes VBA language (at no extra cost). In a nutshell, VBA is a tool that people use to develop the program that controls SOLIDWORKS.

Think about a robot that knows everything about SOLIDWORKS. This robot can read instruction, and it can also operate SOLIDWORKS very fast and accurate. When you want the robot to do something in SOLIDWORKS, you write up a set of robotic instruction by using special codes. Tell the robot to follow your instruction, while you sit back and take some rest. That’s kind of what VBA is all about.


What can you do with VBA?

You probably know that people use any CAD software, not just SOLIDWORKS, for different tasks. Below are some examples:

  • Creating sketches
  • Creating 3D models
  • Creating Simple to Complex assemblies of 3D models
  • Linking files with external softwares mostly excel and many more tasks

This list could go on and on, but you get the idea the idea. My point is simply that a CAD Software, for example, SOLIDWORKS used for wide variety of things. Everyone visiting this blog has different needs and expectations. One thing virtually every visitor has common is the need to automate some aspect of their work. That is what VBA is all about.


What are the advantages and disadvantages of VBA?

In this section, I briefly describe the good things about VBA – and I also explore its darker side.

VBA Advantages

You can automate almost anything you do in SOLIDWORKS. To do so, you write instructions that SOLIDWORKS carries out. Automating a task by using VBA offers several advantages:

  1. SOLIDWORKS always executes the tasks in exactly the same way. (In most cases consistency is good.)
  2. SOLIDWORKS performs the task much faster than you do it manually.
  3. If you are a good macro programmer, SOLIDWORKS always performs the task without error.
  4. If you set things properly, someone who doesn’t know anything about SOLIDWORKS can perform task.
  5. For long, time-consuming tasks, you don’t have to sit in front of your computer and get bored. SOLIDWORKS does work, while you drink water.

VBA disadvantages

It’s only fair that I give equal time to listing the disadvantages (or potential disadvantages) of VBA:

  1. You have to know how to write programs in VBA (but that’s why you are here, right?). Fortunately, it’s not as difficult as you might expect.
  2. Sometimes, things go wrong. In other words, you can’t blindly assume that your VBA program will always work correctly under all circumstances. Welcome to the world of debugging and, if others are using your macros, be prepared for technical support.

VBA in nutshell

Just to let you know what you are in for, I’ve prepared a quick summary of what VBA is all about.

  • You perform actions in VBA by writing (or recording) code in a VBA module. You view and edit VBA modules by using the Visual Basic Editor (VBE).
  • A VBA module consists of Sub procedures. A sub procedure is a chunk of computer code that performs some action on or with objects. The following example shows a simple Sub procedure called AddThem. This amazing program displays the result of 1 plus 1.

Example

Sub AddThem()
    Sum = 1 + 1
    MsgBox ("The answer is " & Sum)
End Sub

No comments:

Post a Comment