visual basic for applications (vba)
DESCRIPTION
Visual Basic for Applications (VBA). An object-oriented programming language Instructions for the manipulation of objects A structured way to provide instructions to Excel Excel has an Object Library that defines its set of objects (e.g. workbooks, charts, ranges of cells). Logic of Language. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/1.jpg)
Visual Basic for Applications (VBA)
• An object-oriented programming language– Instructions for the manipulation of objects– A structured way to provide instructions to
Excel
• Excel has an Object Library that defines its set of objects (e.g. workbooks, charts, ranges of cells)
![Page 2: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/2.jpg)
![Page 3: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/3.jpg)
Logic of Language
• First part of a command identifies the object
• Second part of command:– Sets a property of the object (e.g. color or font)– Takes an action on the object (e.g.copy or
move)– Sets an attribute to the object (e.g. assigns a
value to it using := )
![Page 4: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/4.jpg)
Example of an Instruction
• Application.Workbooks(“Book1.xls”).Worksheets(“Sheet1”).Range(“A1”).Font.Name=“Arial”
• Application.Workbooks(“Book1.xls”).Worksheets(“Sheet1”).Range(“A1”) is the object
• Font.Name=“Arial” is the property being set
![Page 5: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/5.jpg)
Control of VBA
• A Procedure is a set of instructions that operate on a set of objects
• A Function is a procedure that returns a value as a result
• A Subroutine is a procedure that can be run or used by another macro
![Page 6: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/6.jpg)
VBA Toolbar (Excel 2003)
![Page 7: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/7.jpg)
Developer Tab (Excel 2007)
![Page 8: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/8.jpg)
BlueCell Macro
• Sub bluecell()• '• ' bluecell Macro• ' Macro recorded 10/12/2002 by LMU LMU• '• '• With Selection.Interior• .ColorIndex = 8• .Pattern = xlSolid• .PatternColorIndex = xlAutomatic• End With• End Sub
![Page 9: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/9.jpg)
Running Macros
• Run Macro option
• Shortcut Keys: [Ctrl][Shift] letter
• Command Buttons
• Custom Toolbar
![Page 10: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/10.jpg)
Running Solver through a Macro
• Solver dialog box must have been opened at least once in current Excel session (i.e. Solver must be currently activated in Excel)
• SolverSolve UserFinish:=True
• Tools Reference option in VBA must have Solver selected
![Page 11: Visual Basic for Applications (VBA)](https://reader037.vdocument.in/reader037/viewer/2022102908/56812ece550346895d946e06/html5/thumbnails/11.jpg)
Debugging VBA Code
• When VBA encounters a problem, it will highlight line in yellow.
• Use [f8] key to step through code
• Use the Reset icon button (square) at top of VBA to exit step-through mode and trouble- shoot code or Excel/VBA settings. Cannot retry macro until yellow highlight is gone.