0% found this document useful (0 votes)
32 views

Visual Basic For Applications

Uploaded by

Avina Nig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Visual Basic For Applications

Uploaded by

Avina Nig
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Visual Basic for Applications (VBA): Definition, Uses, and Examples

Investopedia / Theresa Chiechi

Definition

Visual Basic for Applications (VBA) is part of Microsoft Corporation's legacy software Visual Basic. VBA is
used to write programs for the Windows operating system.

What Is Visual Basic for Applications (VBA)?

Visual Basic for Applications (VBA) is used to write programs for the Windows operating system. It runs
as an internal programming language in Microsoft Office (MS Office, Office) applications including
Access, Excel, PowerPoint, Publisher, Word, and Visio.

VBA is part of Microsoft Corporation's (MSFT) legacy software Visual Basic. It allows users to customize
beyond what's normally available with MS Office host applications.1

Key Takeaways

 Visual Basic for Applications is a computer programming language developed and owned by
Microsoft.

 You can create macros to automate repetitive word- and data-processing functions with VBA and
generate custom forms, graphs, and reports.

 VBA functions within MS Office applications. It's not a stand-alone product.

 VBA is accessed in Excel by hitting Alt + F11 while having an Excel workbook present.

 VBA leverages objects, variables, properties, projects, logical operators, and modules to make
statements recognizable by debugging processes.

Understanding Visual Basic for Applications (VBA)

VBA is an event-driven tool. You can use it to tell the computer to initiate an action or string of actions by
typing commands into an editing module to build custom macroinstructions (macros).

A macro is essentially a sequence of characters that inputs results in another sequence of characters (its
output). This accomplishes specific computing tasks. You don't have to purchase the VBA software
because VBA is the version of Visual Basic that ships with Microsoft Office.

VBA isn't a stand-alone program. It allows users to manipulate graphical user interface (GUI) features
such as toolbars, menus, dialogue boxes, and forms. You can use VBA to create user-defined functions
(UDFs), access Windows application programming interfaces (APIs), and automate specific computer
processes and calculations.2

Microsoft hosts various learning opportunities for entry-level developers to gain modest experience
working with VBA.3

Microsoft Learn. "Visual Basic Conceptual Topics."

VBA in Excel
All Office suite programs share common programming languages and each is capable of integrating VBA
code to enhance the program. VBA has been a natural fit with Excel more so than with other Office suite
programs because of the repetitive nature of spreadsheets, data analytics, and organizing data.

The root of the relationship between VBA and Excel is often tied to the use of macros. You can use VBA
to run a macro in Excel but you can use it for non-macro activities as well.

How to Access VBA in Excel

Simply press Alt + F11 to access VBA in Excel.4 Your existing Excel workbook will remain running but a
new window will appear for Microsoft Visual Basic for Applications. The top left of the VBA window will
show the current projects. The InvestopediaProject file is ready to receive VBA code in this example:

VBA, Project Window.

The window displays the properties of the selected project at the bottom left. Properties are listed as
projects or workbooks are selected. These properties are listed alphabetically by default although they
can be sorted by category.
VBA Example, Properties.

A new window appears when you double-click on a project on the top left. There's no information in this
area but you'll see two dropdowns that say "(General)" and "(Declarations)." VBA code is directly
entered into this coding window.
VBA Example, Code.

Here's an example of a VBA code that's been entered:


VBA Example, Code with Coding.

Many important buttons and tools appear on the toolbar. The items highlighted in yellow are the run,
break, and reset toggles for the VBA code. The run button executes the code. The break button pauses
the activity of the code. The reset stops the execution of the code and brings the process back to the
starting position of the code.
VBA Example, Toolbar.

What You Can Do With VBA

Finance is about manipulating huge amounts of data and VBA is endemic to the financial services sector.
It's likely running within applications you use each day if you work in finance even if you're not aware of
it. Some jobs in the sector require prior knowledge of VBA and others don't. You can:

 Write macros: Macros allow financial professionals such as accountants, commercial


bankers, investment bankers, research analysts, salesmen, traders, portfolio managers, clerks,
and administrators to analyze and adjust huge amounts of data quickly.

 Update data: You can use VBA in Excel to create and maintain complex trading, pricing, and risk-
management models, to forecast sales and earnings, and to generate financial ratios.

 Perform scenario analysis: You can create various portfolio management and investment
scenarios with Visual Basic for Applications. This includes filtering through situations that may
impact outcomes differently.

 Organize information: You can also use VBA to produce lists of customers’ names or other
content, to create invoices, forms and charts, to analyze scientific data, and to manage data
display for budgets and forecasting.

 Be unconventional: VBA can be used to copy and paste values, adjust cell styles for an entire
workbook, and strike accelerator keys. You can perform very normal tasks in an easier,
automated manner.

 Prompt action: VBA can be used to interact with users. You might need a user's input for their
first and last names to be placed on a form. VBA prompts a user in a way that makes this input
unavoidably mandatory.
Many online forums provide VBA code that allows you to simply copy and paste the code for your
personal use. Be cautious when using someone else's code, especially if you're unfamiliar with the
source, the individual, or the logic of the code.

Important VBA Terms

Module

A module is where Excel stores the VBA code. Information regarding the modules within a spreadsheet
can be found in Project Explorer, one of the sections of the Visual Basic Editor. All modules can be saved
within a modules folder. Modules are sometimes referred to as standard modules.

Objects

Most code is used to manipulate objects in VBA. Objects are items such as workbooks, worksheets, cells,
cell ranges, or cell fonts. Objects are often selected or referred to as part of the code when you're coding
in VBA. The code can use the "ActiveCell" language to manipulate the object currently selected in the
spreadsheet. You can also create a process that executes when a bar chart is edited.

Procedures

The procedure is the part of a computer program that performs a specific task. It's the block of code that
starts with a declaration and finishes with an end declaration. There are two types of procedures in VBA.
Sub procedures form an action in Excel and begin with the text "Sub." Function procedures carry out
calculations and return a value.

Statement

A statement is an instruction that can be broken into two types. First, a declaration statement is used to
state something such as defining a constant or a variable value. Second, executable statements designate
code that specifies what a certain action is.

Variables

Variables are storage locations for defined items. They hold specific values that may change as VBA
scripts are performed. The variable "FirstName" may not contain any value. but it can be assigned the
FirstName variable and given the value "Jo" after the user inputs their name. Variables in coding can be
different depending on the situation, similar to how variable costs can change over time.

Logical Operators

Logical operators are the functions that allow for greater analytical and processing capabilities. They're
bits of code that allow a computer to understand and compare items. VBA can analyze whether the
user's name is "Jo." The program can analyze the input and perform a logical evaluation using logical
operators such as 'if, then', 'true', and 'false.'
Programming

Option Explicit

' This is my global variable to store the dates

Dim storageArray() As Variant

Private Sub ShowData()

' The range the dates are stored in (A1 to A4 on sheet1 in this example)

Dim dateRange As Range

Set dateRange = ThisWorkbook.Sheets("Sheet1").Range("A1:A4")

' Check if the range is empty

If WorksheetFunction.CountA(dateRange) = 0 Then

' The range is empty

' Add the dates back to the range

dateRange.Value = storageArray

Else

' The range is not empty

' Store the dates in the global variable and clear the range

storageArray = dateRange.Value

dateRange.Clear

End If
End Sub

Pro-2

Option Explicit

Private Sub StoreData()

' The range the dates are stored in (A1 to A4 on sheet1 in this example)

Dim dateRange As Range

Set dateRange = ThisWorkbook.Sheets("Sheet1").Range("A1:A4")

' The other range to temporarily store the dates

' Does not have to be on the same sheet

Dim storageRange As Range

Set storageRange = ThisWorkbook.Sheets("Sheet1").Range("C1:C4")

' Check if the range is empty

If WorksheetFunction.CountA(dateRange) = 0 Then

' The range is empty

' Add the dates back to the range

dateRange.Value = storageRange.Value

storageRange.Clear

Else

' The range is not empty


' Store the dates in the other location and clear the range

storageRange.Value = dateRange.Value

dateRange.Clear

End If

End Sub

You might also like