VBA: A BRIEF INTRODUCTION SEAMUS STEGENGA MEEN 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 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 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? DRAWINGREVFILENAME AG GAE1.2AG GAE rev 1.2.DWG AG GAE1AG GAErev1.0.dwg AG GAE1.2AG GAErev1.2.dwg AG GAE1.1AG GAE_1.1.DWG AG GAE1.2AG GAErev1.1.dwg AG GAE1.2AG GAE Rev1.2.dwg AG GAE1AG GAE Rev1 AG GAE1.1AG GAE Rev1.1.dwg AG GAE1AG GAE_1.0.DWG AG GAE1AG GAE_1.0.DWG AG GAE1.1AG GAE Rev1.1.dwg AG GAE2AG GAE Rev1.2 AG GAE1AG GAE_1.DWG AG GAE2AG GAE rev 2.dwg AG GAE1AG GAE rev1.0.dwg AG GAE1AG GAE_1 AG GAE1AG GAErev1.0.DWG AG GAE1AG GAErev1.0.DWG AG GAE1.2AG GAE rev 1.2 AG GAE1AG GAE rev 1.0.DWG AG GAE1AG GAErev1 AG GAE1.1AG GAE Rev1.1.dwg AG GAE1AG GAE rev1 AG GAE1.2AG GAE Rev1.2.dwg AG GAE2AG 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? DRAWINGREVFILENAME AG GAE1.2AG GAE rev 1.2.DWG AG GAE1AG GAErev1.0.dwg AG GAE1.2AG GAErev1.2.dwg AG GAE1.1AG GAE_1.1.DWG AG GAE1.2AG GAErev1.1.dwg AG GAE1.2AG GAE Rev1.2.dwg AG GAE1AG GAE Rev1 AG GAE1.1AG GAE Rev1.1.dwg AG GAE1AG GAE_1.0.DWG AG GAE1AG GAE_1.0.DWG AG GAE1.1AG GAE Rev1.1.dwg AG GAE2AG GAE Rev1.2 AG GAE1AG GAE_1.DWG AG GAE2AG GAE rev 2.dwg AG GAE1AG GAE rev1.0.dwg AG GAE1AG GAE_1 AG GAE1AG GAErev1.0.DWG AG GAE1AG GAErev1.0.DWG AG GAE1.2AG GAE rev 1.2 AG GAE1AG GAE rev 1.0.DWG AG GAE1AG GAErev1 AG GAE1.1AG GAE Rev1.1.dwg AG GAE1AG GAE rev1 AG GAE1.2AG GAE Rev1.2.dwg AG GAE2AG 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 DRAWINGREVFILENAMES AG GAE1.2AG GAE rev 1.2.DWG AG GAE1AG GAErev1.0.dwg AG GAE1.2AG GAErev1.2.dwg AG GAE1.1AG GAE_1.1.DWG AG GAE1.2different!AG GAErev1.1.dwg AG GAE1.2AG GAE Rev1.2.dwg AG GAE1AG GAE Rev1 AG GAE1.1AG GAE Rev1.1.dwg AG GAE1AG GAE_1.0.DWG AG GAE1AG GAE_1.0.DWG AG GAE1.1AG GAE Rev1.1.dwg AG GAE2different!AG GAE Rev1.2 AG GAE1AG GAE_1.DWG AG GAE2AG GAE rev 2.dwg AG GAE1AG GAE rev1.0.dwg AG GAE1AG GAE_1 AG GAE1AG GAErev1.0.DWG AG GAE1AG GAErev1.0.DWG AG GAE1.2AG GAE rev 1.2 AG GAE1AG GAE rev 1.0.DWG AG GAE1AG GAErev1 AG GAE1.1AG GAE Rev1.1.dwg AG GAE1AG GAE rev1 AG GAE1.2AG GAE Rev1.2.dwg AG GAE2AG 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 Web. 11 Feb Martin, Kipp. "Introduction to VBA." University of Chicago: Booth School of Business (2012): n. pag. 4 Jan Web. 11 Feb Power, D. J. "A Brief History of Spreadsheets." DSSResources.com, 30 Aug Web. 11 Feb
QUESTIONS ?
?
?
?
?
?
?
?
?
?
?
?