1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Visual Basic for Applications. What it does Extends the features and built in functions of Excel – Create and run VB procedures – Some may be easy to.
VBA for MS Excel Hamze Msheik. Open the Visual Basic Editor in Excel 2007 Click on the Microsoft Office button in the top left of the Excel window and.
Arrays. What is an Array? An array is a way to structure multiple pieces of data of the same type and have them readily available for multiple operations.
1 CS 106, Winter 2009 Class 5, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
Adding Automated Functionality to Office Applications.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
Object Variables Visual Basic for Applications 3.
1 CS 106 Computing Fundamentals II Chapter 17 “Introduction To VBA” Herbert G. Mayer, PSU CS status 6/30/2013 Initial content copied verbatim from CS 106.
Copyright 2007, Paradigm Publishing Inc. EXCEL 2007 Chapter 7 BACKNEXTEND 7-1 LINKS TO OBJECTIVES Record & run a macro Record & run a macro Save as a macro-
1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 7 “Showing Developer Tab” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from CS.
1 CS 106 Computing Fundamentals II Chapter 4 “Excel Basics for Mac” Herbert G. Mayer, PSU CS status 6/27/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 CS 106 Computing Fundamentals II Chapter 32 “Boolean Expressions” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 84 “Array Formulae” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106.
Spreadsheet Models for Managers: Session 14 14/1 Copyright © Richard Brenner Spreadsheet Models for Managers Session 14 Using Macros II Function.
1 CS 106 Computing Fundamentals II Chapter 75 “Arrays” Herbert G. Mayer, PSU CS Status 7/31/2013 Initial content copied verbatim from CS 106 material developed.
ME 142 Engineering Computation I Using Subroutines Effectively.
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.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables.
A Macro to Exchange the Values of Arbitrary Cells.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
1 CS 106 Computing Fundamentals II Chapter 211 “Coding And Debugging” Herbert G. Mayer, PSU CS status 6/29/2013 Initial content copied verbatim from CS.
1 CS 106 Computing Fundamentals II Chapter 210 “Adding Controls to User Forms” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
1 CS 106 Computing Fundamentals II Chapter 6 “How To Zip & Unzip Files” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
1 CS 106 Computing Fundamentals II Chapter 85 “Excel Tables” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
IE 8580 Module 4: DIY Monte Carlo Simulation
EGR 2261 Unit 11 Pointers and Dynamic Variables
CS 106 Computing Fundamentals II Chapter 5 “Excel Basics for Windows”
VBA - Excel VBA is Visual Basic for Applications
CS 106 Computing Fundamentals II Chapter 35 “Controls For Choices”
CS 106 Computing Fundamentals II Chapter 77 “Algorithm”
CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”
Pointers and Pointer-Based Strings
Microsoft Office Illustrated
Exploring Microsoft Excel
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CS 106 Computing Fundamentals II Chapter 73 “Ranges”
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
Fundamentals of visual basic
Herbert G. Mayer, PSU CS Status 8/2/2013
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
CS 106 Computing Fundamentals II Chapter 64 “For Loop Variations”
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
Pointers and Pointer-Based Strings
Relational Operators.
Activate a range and manipulating activated range
Variables and Constants
Excel VBA Programming © Copyright 2011, TechMentors All rights reserved.
Presentation transcript:

1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

2 Syllabus Variables Variables Assignment Statement Assignment Statement NOT An Equation NOT An Equation Exchanging Values Exchanging Values Macro Exchange A1B1 Macro Exchange A1B1

3 Variables To represent values needed by a process, we’ll use variablesTo represent values needed by a process, we’ll use variables Variables refer to a location where a value can be stored, rather than a particular value; the value of a variable can change, hence the nameVariables refer to a location where a value can be stored, rather than a particular value; the value of a variable can change, hence the name A variable designates a location of a computer’s memoryA variable designates a location of a computer’s memory 3

4 Assignment Statement A VBA program is at times made up mostly of statementsA VBA program is at times made up mostly of statements The assignment statement assigns a value to a variableThe assignment statement assigns a value to a variable Examples:Examples: x = 7‘ assign 7 to x, type integer name = “Cindy”‘ assign the name Cindy to name, type string x = x + 1‘ assign x 1 more than before, type integer

5 NOT an Equation! In the statementIn the statement x = 4 the x refers to the memory location where the value 4 is to be stored, but after the assignment the two are indeed equalthe x refers to the memory location where the value 4 is to be stored, but after the assignment the two are indeed equal The meaning is, store the value 4 in the memory location designated by xThe meaning is, store the value 4 in the memory location designated by x Perhaps a more obvious syntax to document the assignment would be to write:Perhaps a more obvious syntax to document the assignment would be to write: x  4 But that is not how VBA defines assignmentsBut that is not how VBA defines assignments

6 Left Side, Right Side In the assignment statementIn the assignment statement x = x + 1 the x on the left refers to the memory location where a value will be storedthe 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 xVBA 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 The right hand side is evaluated first, and the computed result then moved to variable on the leftThe right hand side is evaluated first, and the computed result then moved to variable on the left

7 Order is Important! Consider the following sequence of statements: 1. x = 4location x now contains 4 2. y = 7location y now contains 7 3. x = x + ylocation x now contains y = 3location y now contains 3 5. z = x + ylocation z now contains 14 x is still 11 (no new assignment to x after receiving 11) The value of expression x + y changed when we changed y in line 4 Does NOT change the value of x as assigned in line 3

8 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 = y 2.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 line 1 was executed!

9 The Fix We need an extra variable to store one value while we exchange the other. Let’s call it temp: 1.temp = x 2.x = y 3.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

10 Excel Exchanges of Cell Values The workbook called ExchangeCellValues contains a macro called ExchangeA1B1 that exchanges the values of those two cells The workbook called ExchangeCellValues contains a macro called ExchangeA1B1 that exchanges the values of those two cells The code is on the next slide. Note: The code is on the next slide. Note: Used a banner comment to describe what the macro does Used comments within the code, too Indented lines to make everything more readable The VBA editor colored comments (green) and keywords (blue) New way of referring to cells, Cells(n,m) Dim statement to declare variable temp

11 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 'Use a Variant data type so this works for any values in 'the two cells 'the two cells Dim temp As Variant Dim temp As Variant temp = Cells(1, 1).Value temp = Cells(1, 1).Value Cells(1, 1).Value = Cells(1, 2).Value Cells(1, 1).Value = Cells(1, 2).Value Cells(1, 2).Value = temp Cells(1, 2).Value = temp End Sub

12 The Cells(n,m) Notation Referring to cells by names like A1 and B1 is convenient for humans but not as good for programmingReferring 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 mIn 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. Be careful to avoid errors with reversing the numbersNote the unfortunate fact that the Cells notation puts the row first and the column second, while the A1 notation does the reverse. Be careful to avoid errors with reversing the numbers

13 Demo: ExchangeA1B1 Run the ExchangeA1B1 macro. Don’t forget to enable macros when you open the ExchangeCellValues workbook.