32
VBA: A BRIEF INTRODUCTION SEAMUS STEGENGA MEEN 381-506 FEBRUARY 11, 2015

VBA: A BRIEF INTRODUCTION SEAMUS STEGENGA MEEN 381-506 FEBRUARY 11, 2015

Embed Size (px)

Citation preview

VBA: A BRIEF INTRODUCTION

SEAMUS STEGENGA

MEEN 381-506

FEBRUARY 11, 2015

OVERVIEW

Introduction…………… 1 slide

History………………… 2 slides

Hypothetical example… 10 slides

Real-world example…… 2 slides

Pros and cons ………… 1 slide

Conclusion …………… 1 slide

Questions……………… 1 slide

INTRODUCTION

VBA is a proprietary object-oriented programming language developed by Microsoft

It is built in to MS office installations (Anyone with Excel already has it installed on their computer)

It allows users to automate tasks, manipulate large data sets, and create custom interfaces

HISTORY

Early computing had many programming languages Problem: steep learning curve

In 1975, Bill Gates dropped out of Harvard to create an interpreter for BASIC

Product was instant success, becoming the central product of his company, Microsoft

Independently, VisiCal, the first spreadsheet program, was created in 1978 It was also an instant success, becoming the first “killer app” and substantially boosting PC sales

Lotus 1-2-3 soon followed, becoming the dominate entry in the spreadsheet market

Microsoft tried to compete with Microplan (predecessor to Excel), but generally failed to gain market share

HISTORY

15 years later, Microsoft was still struggling to gain market share

Then, in 1993, Microsoft combined its BASIC interpreter with Excel The result was VBA, an object-oriented language centered around the MS office suite

VBA-based Excel was successful beyond even Microsoft’s expectations,

Lotus 1-2-3 lost dominate market position in 1995 and was bought by IBM later that year

Since then, Excel has become a de-facto standard in business settings (especially engineering)

EXAMPLE: PROBLEM STATEMENT

Suppose that on a particular project, your boss tells you to check which AutoCAD drawings have been updated and need to be re-sent to the client. They give you a list of drawings, the current revision of each drawing, and a list of filenames that have already been sent.

Which drawings need to be resent?

DRAWING REV FILENAMEAG10-4637-GAE 1.2

AG10-4637-GAE rev 1.2.DWG

AG11-5620-GAE 1 AG11-5620-GAErev1.0.dwgAG12-4004-GAE 1.2 AG12-4004-GAErev1.2.dwgAG14-9294-GAE 1.1 AG14-9294-GAE_1.1.DWGAG15-1937-GAE 1.2 AG15-1937-GAErev1.1.dwgAG15-3441-GAE 1.2 AG15-3441-GAE Rev1.2.dwgAG16-7964-GAE 1 AG16-7964-GAE Rev1AG17-7538-GAE 1.1 AG17-7538-GAE Rev1.1.dwgAG20-1013-GAE 1 AG20-1013-GAE_1.0.DWGAG22-6293-GAE 1 AG22-6293-GAE_1.0.DWGAG22-6939-GAE 1.1 AG22-6939-GAE Rev1.1.dwgAG23-9721-GAE 2 AG23-9721-GAE Rev1.2AG26-1252-GAE 1 AG26-1252-GAE_1.DWGAG26-3982-GAE 2 AG26-3982-GAE rev 2.dwgAG26-7808-GAE 1 AG26-7808-GAE rev1.0.dwgAG28-4049-GAE 1 AG28-4049-GAE_1AG30-4827-GAE 1 AG30-4827-GAErev1.0.DWGAG31-9504-GAE 1 AG31-9504-GAErev1.0.DWGAG34-6161-GAE 1.2 AG34-6161-GAE rev 1.2AG37-2632-GAE 1

AG37-2632-GAE rev 1.0.DWG

AG37-4679-GAE 1 AG37-4679-GAErev1AG37-4922-GAE 1.1 AG37-4922-GAE Rev1.1.dwgAG39-4773-GAE 1 AG39-4773-GAE rev1AG42-1418-GAE 1.2 AG42-1418-GAE Rev1.2.dwgAG44-6605-GAE 2 AG44-6605-GAErev2.dwg

EXAMPLE: PROBLEM STATEMENT

Suppose that on a particular project, your boss tells you to check which AutoCAD drawings have been updated and need to be re-sent to the client. They give you a list of drawings, the current revision of each drawing, and a list of filenames that have already been sent.

Which drawings need to be resent?

DRAWING REV FILENAMEAG10-4637-GAE 1.2

AG10-4637-GAE rev 1.2.DWG

AG11-5620-GAE 1 AG11-5620-GAErev1.0.dwgAG12-4004-GAE 1.2 AG12-4004-GAErev1.2.dwgAG14-9294-GAE 1.1 AG14-9294-GAE_1.1.DWGAG15-1937-GAE 1.2 AG15-1937-GAErev1.1.dwgAG15-3441-GAE 1.2 AG15-3441-GAE Rev1.2.dwgAG16-7964-GAE 1 AG16-7964-GAE Rev1AG17-7538-GAE 1.1 AG17-7538-GAE Rev1.1.dwgAG20-1013-GAE 1 AG20-1013-GAE_1.0.DWGAG22-6293-GAE 1 AG22-6293-GAE_1.0.DWGAG22-6939-GAE 1.1 AG22-6939-GAE Rev1.1.dwgAG23-9721-GAE 2 AG23-9721-GAE Rev1.2AG26-1252-GAE 1 AG26-1252-GAE_1.DWGAG26-3982-GAE 2 AG26-3982-GAE rev 2.dwgAG26-7808-GAE 1 AG26-7808-GAE rev1.0.dwgAG28-4049-GAE 1 AG28-4049-GAE_1AG30-4827-GAE 1 AG30-4827-GAErev1.0.DWGAG31-9504-GAE 1 AG31-9504-GAErev1.0.DWGAG34-6161-GAE 1.2 AG34-6161-GAE rev 1.2AG37-2632-GAE 1

AG37-2632-GAE rev 1.0.DWG

AG37-4679-GAE 1 AG37-4679-GAErev1AG37-4922-GAE 1.1 AG37-4922-GAE Rev1.1.dwgAG39-4773-GAE 1 AG39-4773-GAE rev1AG42-1418-GAE 1.2 AG42-1418-GAE Rev1.2.dwgAG44-6605-GAE 2 AG44-6605-GAErev2.dwg

EXAMPLE: SOLUTION

Pressing Alt+F11 pulls up the VBA editor

EXAMPLE: SOLUTION

Double clicking “ThisWorkbook” brings up a white text editor

EXAMPLE: SOLUTION

Typing “Sub example_name” and pressing enter creates a new sub-routine

Code is automatically color-coded as you type

EXAMPLE: SOLUTION

Write your macro cells(2,3).value => value of cell C2

UCase(“string”) => “STRING”

Right(“string”,4) => “ring”

CDec(“1.49”) => 1.49

Replace(“i pie”,”i”,”U”) => “U pUe”

Typing the function name and open parenthesis will make a tooltip pop up, telling you what inputs the function is expecting.

This is very useful!

Most functions can be found using online search

EXAMPLE: SOLUTION

Clicking the green triangle on the top window executes the macro (can also start by pressing F5)

Afterwards, the Excel pane looks like the following

DRAWING REV FILENAMESAG10-4637-GAE 1.2

AG10-4637-GAE rev 1.2.DWG

AG11-5620-GAE 1 AG11-5620-GAErev1.0.dwgAG12-4004-GAE 1.2 AG12-4004-GAErev1.2.dwgAG14-9294-GAE 1.1 AG14-9294-GAE_1.1.DWGAG15-1937-GAE 1.2 different! AG15-1937-GAErev1.1.dwgAG15-3441-GAE 1.2 AG15-3441-GAE Rev1.2.dwgAG16-7964-GAE 1 AG16-7964-GAE Rev1AG17-7538-GAE 1.1 AG17-7538-GAE Rev1.1.dwgAG20-1013-GAE 1 AG20-1013-GAE_1.0.DWGAG22-6293-GAE 1 AG22-6293-GAE_1.0.DWGAG22-6939-GAE 1.1 AG22-6939-GAE Rev1.1.dwgAG23-9721-GAE 2 different! AG23-9721-GAE Rev1.2AG26-1252-GAE 1 AG26-1252-GAE_1.DWGAG26-3982-GAE 2 AG26-3982-GAE rev 2.dwgAG26-7808-GAE 1 AG26-7808-GAE rev1.0.dwgAG28-4049-GAE 1 AG28-4049-GAE_1AG30-4827-GAE 1 AG30-4827-GAErev1.0.DWGAG31-9504-GAE 1 AG31-9504-GAErev1.0.DWGAG34-6161-GAE 1.2 AG34-6161-GAE rev 1.2AG37-2632-GAE 1

AG37-2632-GAE rev 1.0.DWG

AG37-4679-GAE 1 AG37-4679-GAErev1AG37-4922-GAE 1.1 AG37-4922-GAE Rev1.1.dwgAG39-4773-GAE 1 AG39-4773-GAE rev1AG42-1418-GAE 1.2 AG42-1418-GAE Rev1.2.dwgAG44-6605-GAE 2 AG44-6605-GAErev2.dwg

EXAMPLE: ADDING FEATURES

If desired, you can also make Excel relay information using a variety of pop-ups

For example, 9 lines were added to the previous code, as shown on right

EXAMPLE: ADDING FEATURES

If desired, you can also make Excel relay information using a variety of pop-ups

For example, 9 lines were added to the previous code, as shown on right The result is a text pop-up listing the out-of-date drawings This can be copied directly into

Outlook!

EXAMPLE: FINAL WORD

VBA is helpful with:

Large data sets

Repetitive tasks

Interacting with other programs

Removing sheet protection

Hiding formulas

Dynamic links to external files

REAL-WORLD EXAMPLE

Real-world example:

BOM => requisition forms

REAL-WORLD EXAMPLE

Without VBA: Sort by manufacturer

Copy data, paste without formatting

Each copy-paste repeated ~15 times per BOM

Error prone

With VBA: Single button added to ribbon

A single click creates and saves one requisition form per vendor

Fast and low error rate

Furthermore, errors are not the kinds that “slip through the cracks”

4 hour process => 5 minute process!

AUTOMATING EXCEL: PROS AND CONS

PROS• Completes complex tasks quickly• Allows for greater control over Excel• Reduces errors

CONS• Large initial time investment• Typically not flexible• Encourages overreliance on technology

CONCLUSION

VBA is an important part of Microsoft’s history

can speed up repetitive tasks and add features to Excel

is easy to learn and has a helpful online community

requires a large initial time investment but can save hundreds of man-hours!

REFERENCES

"Is Visual Basic That Basic?" What Is Visual Basic? Software Engineer Insider, Nov. 2012. Web. 11 Feb. 2015.

Martin, Kipp. "Introduction to VBA." University of Chicago: Booth School of Business (2012): n. pag. 4 Jan. 2012. Web. 11 Feb. 2015.

Power, D. J. "A Brief History of Spreadsheets." DSSResources.com, 30 Aug. 2004. Web. 11 Feb. 2015.

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?

QUESTIONS

?