variables and the assignment statement. basics of variables to represent any values that a process...

12
Variables and the Assignment Statement

Upload: grant-simpson

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

Variables and the Assignment Statement

Page 2: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

2

Basics of Variables

• To represent any values that a process needs to remember, we use variables

• Recall that variables refer to a location where a value can be stored, rather than a particular value; the value of a variable can change

• A variable designates an area of computer memory

Page 3: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

The Assignment Statement

• A VBA program is often made up mostly of statements

• The assignment statement assigns a value to a variable

• Here are a few examples:x = 7name = “Cindy”x = x + 1

Page 4: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

It’s NOT an Equation!

In the statementx = 4

the x refers to the memory location where the value 4 is to be stored

The meaning is, store the value 4 in the memory location designated by x

Page 5: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

Left side, right side

In the assignment statementx = x + 1

the x on the left refers to the memory location where a value will be stored

VBA first evaluates the right side of the statement. It gets the current value from memory location x, adds 1 to it, and stores the result back in memory location x

Page 6: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

Order of Statements is Important!

• Consider the following sequence of statements:1. x = 4 location x now contains 42. y = 7 location y now contains 7 3. x = x + y location x now contains 114. y = 3 location y now contains 35. z = x + y location z now contains 14x is still 11 (no new assignment statement to x)

• The value of expression x + y changed when we changed y in line 4. This does NOT change the value of x as assigned in line 3.

Page 7: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

Exchanging Values of Variables

Suppose I have variables x and y and I want to interchange their values. So if x = 4 and y = 5, I want to end up with x = 5 and y = 4. Here is what I might write at first:

1. x = y2. y = x

Line 1 assigns the value 5 to x. Now that x is 5, Line 2 assigns the value 5 to y. The 4 was lost when I did line 1!

Page 8: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

The Fix

We need an extra variable to store one value while we exchange the other. Let’s call it temp:

1. temp = x2. x = y3. y = temp

So now, temp gets the value 4 in line 1. Then x gets the value 5 in line 2, and y gets the value 4 in line 3

Page 9: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

An Excel Macro That Exchanges Values of Two Cells

• The workbook called ExchangeCellValues contains a macro called ExchangeA1B1 that exchanges the values of those two cells.

• The code I wrote is on the next slide. Note that– I used a banner comment to describe what the macro does– I used comments within the code, too– I indented the lines to make everything more readable– The VBA editor colored my comments (green) and keywords

(blue)– I used a new way of referring to cells, Cells(n,m)– I used a Dim statement to declare variable temp

Page 10: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

The Macro ExchangeA1B1'*************************************************************' Exchange the values in Cell(1,1) and Cell(1,2)'*************************************************************Sub ExchangeA1B1() 'Use a Variant data type so this works for any values in 'the two cells Dim temp As Variant

temp = Cells(1, 1).Value Cells(1, 1).Value = Cells(1, 2).Value Cells(1, 2).Value = temp End Sub

Page 11: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

The Cells(n,m) Notation

• Referring to cells by names like A1 and B1 is convenient for humans but not as good for programming

• In programming we’ll use Cells(1,1) for A1, Cells(1,2) for B1, Cells(n,m) for row n, column m

• Note the unfortunate fact that the Cells notation puts the row first and the column second, while the A1 notation does the reverse. You will need to be careful to avoid errors with reversing the numbers

Page 12: Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables

Demo: ExchangeA1B1

Run the ExchangeA1B1 macro. Don’t forget to enable macros when you open

the ExchangeCellValues workbook.