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

Slides:



Advertisements
Similar presentations
Creating Tables The basics, nothing pretty, but it works.
Advertisements

Computer Programming w/ Eng. Applications
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
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.
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
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.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
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.
Implementation of a Stored Program Computer
Adding Automated Functionality to Office Applications.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
Introduction to Excel VBA University of Chicago Graduate School of Business Introduction to Computer Based Models Bus Mr. Schrage Spring 2003.
Macros n Macros are little programs that you can create to automate particular tasks that you may want to execute more easily than having to specify all.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...
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-
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
 An object-oriented programming language ◦ Instructions for the manipulation of objects ◦ A structured way to provide instructions to Excel  Excel has.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Visual Basic for Applications Macro Programming For Microsoft Office.
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.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Mathematical Calculations in Java Mrs. G. Chapman.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 9 Macros And Visual Basic For Applications.
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.
Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification.
Mathematical Calculations in Java Mrs. C. Furman.
ME 142 Engineering Computation I Using Subroutines Effectively.
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?
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Controlling Program Flow with Decision Structures.
Ranges. Unlike many of our programming concepts, the idea of a Range is particular to Excel The ideas and code discussed in these slides can be found.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
2.4 – Solving Equations with the Variable on Each Side.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
IE 8580 Module 4: DIY Monte Carlo Simulation
EGR 2261 Unit 11 Pointers and Dynamic Variables
VBA - Excel VBA is Visual Basic for Applications
CS 1110 Introduction to Programming Spring 2017
Microsoft Office Illustrated
Cracking the Coding Interview
Conditions and Ifs BIS1523 – Lecture 8.
Exploring Microsoft Excel
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Unit-1 Introduction to Java
Solving Equations (Addition & Subtraction)
Fundamentals of visual basic
CISC124 Labs start this week in JEFF 155.
CHAPTER FOUR VARIABLES AND CONSTANTS
CS 1111 Introduction to Programming Spring 2019
Activate a range and manipulating activated range
Completing the Square.
Excel VBA Programming © Copyright 2011, TechMentors All rights reserved.
Microsoft Excel 2007 – Level 2
Presentation transcript:

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 A variable designates an area of computer memory 2

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: 1.x = 4location x now contains 4 2.y = 7location y now contains 7 3.x = x + ylocation x now contains 11 4.y = 3location y now contains 3 5.z = x + ylocation 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: 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 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: 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

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.