1 / 13

Visual Basic for Applications (VBA)

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.

idalia
Download Presentation

Visual Basic for Applications (VBA)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. 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 := )

  3. 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

  4. 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

  5. VBA Toolbar (Excel 2003)

  6. Developer Tab (Excel 2007)

  7. BlueCell Macro Sub bluecell() ' ' bluecell Macro ' With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 16777062 .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub

  8. Running Macros • Run Macro option • Shortcut Keys: [Ctrl][Shift] letter • Command Buttons • Custom Toolbar

  9. 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) • SolverSolveUserFinish:=True • Tools Reference option in VBA must have Solver selected • Other VBA commands for controlling Solver in class handout

  10. 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.

  11. Plotting the Efficient Frontier – Version 1A Command Button Private Sub EfficientFrontier_Click() Range("C53") = Range("B59") SolverSolveUserFinish:=True Range("D59") = Range("C51") End Sub

  12. Plotting the Efficient Frontier – Version 1B Command Button Private Sub EfficientFrontier_Click() For unit = 1 To 11 Range("C53") = Range("B" & 58 + unit) SolverSolveUserFinish:=True Range("D" & 58 + unit) = Range("C51") Next unit End Sub

More Related