10
Controls and Events

Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

Embed Size (px)

Citation preview

Page 1: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

Controls and Events

Page 2: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

The Next Step

• In the first module, we discussed general problem solving

• In this module, we’ll apply what we learned, from specification to design to implementation and testing, on a very simple process

• Later we’ll gradually introduce VB features that let us implement more complex processes

Page 3: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

But First…

• We have to learn enough about VBA and Excel to write an interesting program!

• So our goal in this module is to learn those basics and apply them to a problem

Page 4: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

Relating VBA to Processes

• We will typically represent the objects in our process specification by Excel or VBA controls and other Excel and VBA objects

• Objects and controls have events associated with them

• The process events will be translated into control events for which we will write procedures

Page 5: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

Writing a Macro

• We’re going to start by writing (not recording) a small macro with a button to control it

• This will introduce a number of basic concepts

Page 6: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

The Code

Option Explicit'*****************************************' Turn the active cell red'*****************************************Sub RedCell() ActiveCell.Interior.Color = vbRedEnd Sub

Page 7: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

The Video

• You can watch the steps of writing the macro and adding the button on the video called Button Demo

• The workbook is available on the class site• After you watch, try writing your own macro,

say GreenButton, in the same worksheet. Give it a button and/or a piece of clip art to activate it

Page 8: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

Events

• Clicking the button or the clip art is an event that triggers the execution of the macro

• This is a pattern we will continue to follow: our macros will be triggered by events that happen in the spreadsheet

• Later we will also write macros that can be called by other macros

Page 9: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

Referring to Objects and Properties

• Consider the expression ActiveCell.Interior.Color = vbRed• ActiveCell is an object recognized by Excel: the cell

that has been selected in the active worksheet• Interior is a subobject of the ActiveCell: it’s just

the interior of the cell• Color is a property of the Interior, and as such can

be set to a value by an assignment statement

Page 10: Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification

Properties vs Variables

• We’ll often be setting the values of properties of objects

• Cells in particular have a property called the Value: ActiveCell.Value = 5 will make the number 5 show up in the cell

• Besides properties of objects, we’ll also use more general variables for quantities we want our program to remember and work with