Presentation is loading. Please wait.

Presentation is loading. Please wait.

# 1# 1 VBA Objects, Message Boxes as Functions What is an object in VBA? How do you move between design mode and run mode? How can you make a cell become.

Similar presentations


Presentation on theme: "# 1# 1 VBA Objects, Message Boxes as Functions What is an object in VBA? How do you move between design mode and run mode? How can you make a cell become."— Presentation transcript:

1

2 # 1# 1 VBA Objects, Message Boxes as Functions What is an object in VBA? How do you move between design mode and run mode? How can you make a cell become the active cell? CS 105 Spring 2010

3 # 2# 2 Objects—in Excel: OBJECTS Objects represent program entities, or elements that the program manipulates. e.g. a Worksheet, a Chart a Command Button, a Range Objects have _PROPERTIES_ (such as caption, name) and _METHODS_ (such as select, delete, add, clear). We name objects and refer to them by their name.

4 # 3# 3 CS 105 Spring 2010 Naming convention for Objects Command button = cmd cmdExit Label = lbl lblHelpMessage Check box = chk chkReadOnly See your Course Guide

5 # 4# 4 Excel CS 105 Spring 2010 Click the Office button and then click on Excel Options. Excel displays the Excel Options dialog box.

6 # 5# 5 CS 105 Spring 2010 Cells Notation Cells(3,1).Value refers to R3C1 below Remember, row first, then column B3 is the same as Cells(3,2)

7 # 6# 6 CS 105 Spring 2010 Help on cell notation One way to identify a cell is through Cells notation: Range("B1").Value = 8 Range(“E2").Value = 8 Can also be written: Cells(1,2).Value = 8 Cells(2,5).Value = 8

8 # 7# 7 CS 105 Spring 2010 Properties and Methods Properties Range("A1:A10").Value = 99 The Value property is what the cells hold, in this case 99 Methods We can use Range(“A1”).Select This makes “A1” the active cell

9 # 8# 8 How to you make Cells (1, 1) become the active cell? A. Click on Cell A1 B. In your code, write Range(“A1”).Select C. In your code, write Range(“A1”).Activate CS 105 Spring 2010

10 # 9# 9 Clearing a column You can clear a column using the ClearContents method: Private Sub cmdClear_Click() Range("D:D").ClearContents End Sub Note: ClearContents does not clear the formatting, just the numbers or formulas contained within the cells. What code would you use to clear the contents of a range?

11 # 10 CS 105 Spring 2010 Getting Feedback

12 # 11 CS 105 Spring 2010 Using the Message Box as a Function Just as we use the Input Box to get data, we can use the Message Box to gather data from the user

13 # 12 CS 105 Spring 2010 What is the difference? MsgBox "You are now leaving",, "Bye" Cells(1,2) Value = MsgBox(“Quit?”, vbYesNo, “Bye?”)

14 # 13 CS 105 Spring 2010 Using the Message Box to Get Data Private Sub cmdTaxi_Click() Cells(2,2).Value = MsgBox("Hello", vbYesNo, _ "Do you want a taxi?") If Cells(2,2).Value = vbYes Then Range("A1").Value = "Yes" End If If Cells(2,2).Value = vbNo Then Range(“B1").Value = "No" End If End Sub

15 # 14 CS 105 Spring 2010 The Message Box Also Returns a Number Private Sub cmdNumber_Click() Cells(3,2).Value = __ MsgBox("Make a choice", vbYesNo, " Yes or NO? " ) End Sub

16 # 15 CS 105 Spring 2010 vbOK, vbCancel, etc. vbOK 1 vbCancel 2 vbAbort 3 vbRetry 4 vbIgnore 5 vbYes 6 vbNo 7 The Message Box buttons return a value that can be used in programming. 1 2

17 # 16 CS 105 Spring 2010 To Summarize: What is an object in VBA? How do you move between design mode and run mode? How can you make a cell become the active cell?

18 # 17 You are so Right!!!! CS 105 Spring 2010


Download ppt "# 1# 1 VBA Objects, Message Boxes as Functions What is an object in VBA? How do you move between design mode and run mode? How can you make a cell become."

Similar presentations


Ads by Google