1 / 17

VBA in Excel

VBA in Excel. Walter Milner. Introduction. VBA = Visual Basic for Applications Enables end-user programming In MS Office applications Formulae and macros OK for simple actions, but.. Advantages over formulae and macros: Multiple nested if is easy Loops easy Debugging easy.

ehren
Télécharger la présentation

VBA in Excel

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. VBA in Excel Walter Milner

  2. Introduction • VBA = Visual Basic for Applications • Enables end-user programming • In MS Office applications • Formulae and macros OK for simple actions, but.. • Advantages over formulae and macros: • Multiple nested if is easy • Loops easy • Debugging easy

  3. Getting started View..Toolbars..Control

  4. Add a button Click the button button Drag on sheet to create one

  5. Format it Right click on button Select properties Set as required Note distinction between name and caption

  6. Program it Right click button Select View code:

  7. Test it On Control Toolbox, click set square This toggles run/design Click the button

  8. Referring to cells Private Sub CommandButton1_Click() Dim x As Integer Dim y As Integer Dim z As Integer Dim result As Integer x = Cells(1, 2).Value y = Cells(2, 2).Value z = Cells(3, 2).Value result = x * y + z Cells(4, 2).Value = result End Sub

  9. The VBA Excel Object model • Hierarchy of classes • Use to refer to things in an Excel application • Very large number of classes, properties and methods • Have to use on-screen help for reference

  10. The VBA Excel Object model • Object is a thing • Property is some property of an object • Method is something the object can be told to do • Event is something that can happen to the object • Collection is a set of related objects • Enumeration is action returning complete set of things (eg all fonts on system)

  11. Example object - Application

  12. Example properties of Application Program a button which sets a value for this property and see what happens Try it in a loop for a bizarre effect

  13. Example method Program a button which calls the findfile method of the application

  14. Example collection - Display the count property of the worksheets collection in a msgbox to show how many sheets there are in the workbook

  15. Using For Each .. Next in a collection Private Sub CommandButton4_Click() Dim w As Worksheet For EachwIn Application.Worksheets MsgBox (w.Name) Next End Sub

  16. The RangeSelection property of the ActiveWindow This is a Range object So it has the properties of a Range object Use them to program a button which displays the total of the numbers in the cells selected

  17. 2d array exercise Use the RangeSelection property to program a button which does a vertical flip of selected cells

More Related