Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106.

Similar presentations


Presentation on theme: "1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106."— Presentation transcript:

1 1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

2 2 Syllabus Next Step Next Step Relating to VBA Processes Relating to VBA Processes Macros Macros Code Code Events Events Referring to Objects, Properties Referring to Objects, Properties

3 3 Next Step Previously, we discussed general problem solvingPreviously, we discussed general problem solving In this module, we’ll apply what we learned, from specification to design to implementation and testing, to VBA on a very simple processIn this module, we’ll apply what we learned, from specification to design to implementation and testing, to VBA on a very simple process Later we’ll gradually introduce VB features that let us implement more complex processesLater we’ll gradually introduce VB features that let us implement more complex processes

4 4 But First… We have to learn enough about VBA and Excel to write an interesting program!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 problemSo our goal in this module is to learn those basics and apply them to a problem

5 5 Relating VBA to Processes We will typically represent the objects in our process specification by Excel or VBA controls and other Excel and VBA objectsWe 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 themObjects and controls have events associated with them The process events will be translated into control events for which we will write proceduresThe process events will be translated into control events for which we will write procedures

6 6 Macros We’re going to start by writing (not recording) a small macro with a button to control itWe’re going to start by writing (not recording) a small macro with a button to control it This will introduce a number of basic conceptsThis will introduce a number of basic concepts

7 7 Code Option Explicit '***************************************** ' Turn the active cell red '***************************************** Sub RedCell() ActiveCell.Interior.Color = vbRed ActiveCell.Interior.Color = vbRed End Sub

8 8 The Video You can watch the steps of writing the macro and adding the button on the video called Button DemoYou can watch the steps of writing the macro and adding the button on the video called Button Demo The workbook is available on the Spring 2013 class siteThe workbook is available on the Spring 2013 class site After you watch, try writing your own macro, say GreenButton, in the same worksheetAfter 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 itGive it a button and/or a piece of clip art to activate it

9 9 Events Clicking the button or the clip art is an event that triggers the execution of the macroClicking 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 spreadsheetThis 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 macrosLater we will also write macros that can be called by other macros

10 10 Referring to Objects, Properties Consider the expressionConsider the expression ActiveCell.Interior.Color = vbRed ActiveCell is an object recognized by Excel: the cell that has been selected in the active worksheetActiveCell 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 cellInterior 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 statementColor is a property of the Interior, and as such can be set to a value by an assignment statement

11 11 Properties vs. Variables We’ll often set values of properties of objectsWe’ll often set 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 cellCells 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 withBesides properties of objects, we’ll also use more general variables for quantities we want our program to remember and work with


Download ppt "1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106."

Similar presentations


Ads by Google