130 likes | 408 Views
Variables and the Assignment Statement. 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
E N D
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
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 = 7 name = “Cindy” x = x + 1
It’s NOT an Equation! In the statement x = 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
Left side, right side In the assignment statement x = 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
Order of Statements is Important! • Consider the following sequence of statements: • x = 4 location x now contains 4 • y = 7 location y now contains 7 • x = x + y location x now contains 11 • y = 3 location y now contains 3 • z = x + y location z now contains 14 x 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.
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: • x = y • 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!
The Fix We need an extra variable to store one value while we exchange the other. Let’s call it temp: • temp = x • x = y • 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
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
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
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
Demo: ExchangeA1B1 Run the ExchangeA1B1 macro. Don’t forget to enable macros when you open the ExchangeCellValues workbook.