A Macro to Exchange the Values of Arbitrary Cells.

Slides:



Advertisements
Similar presentations
ISOM3230 Business Applications Programming
Advertisements

PROGRAMMING IN VISUAL BASIC PART 1
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
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.
Some computer fundamentals and jargon Memory: Basic element is a bit – value = 0 or 1 Collection of “n” bits is a “byte” Collection of several bytes is.
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.
Programming in Visual Basic
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
String Variables Visual Basic for Applications 4.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
CS0004: Introduction to Programming Variables – Numbers.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
Mr C Johnston ICT Teacher BTEC IT Unit 06 - Lesson 05 Learning To Program.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
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.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Lab 01 Forms in excel Tahani ALdweesh Insert form into your project. 2. Change form’s properties. 3. Put controls on the form. 4. Change controls’
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.
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.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
The Object Model. You can think of the contents of an Excel application as a hierarchy of collections of objects, manipulated by code Each object can.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
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.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Variables and the Assignment Statement. Basics of Variables To represent any values that a process needs to remember, we use variables Recall that variables.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
ME 142 Engineering Computation I Using Subroutines Effectively.
# 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?
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Working with Strings. String Structure A string in VBA can be variable length and has an internal structure Each character in the string has a position.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
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 Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Scope. Scope of Names In a small program written by one person, it is easy to be sure that each variable has a unique name In a large program, or one.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
VBA - Excel VBA is Visual Basic for Applications
Spreadsheet-Based Decision Support Systems
Pointers and Pointer-Based Strings
Microsoft Office Illustrated
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Object Oriented Programming COP3330 / CGS5409
أ.إسراء الطريقي أ. هاله الشملان , 102 تقن , المعمل الخامس
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
CHAPTER FOUR VARIABLES AND CONSTANTS
Intro to Programming Concepts
Pointers and Pointer-Based Strings
B065: PROGRAMMING Variables 2.
Variables and Constants
Tutorial 11 Using and Writing Visual Basic for Applications Code
SPL – PS2 C++ Memory Handling.
Parameters and Arguments
Presentation transcript:

A Macro to Exchange the Values of Arbitrary Cells

What if I want to exchange any two cells? This is a lot harder! But… it will let us introduce some features of VBA The code is in the ExchangeCellValues workbook We’ll look at it bit by bit to understand how it works The basic concept is the same You are not required to understand this material for the course, but it does illustrate some interesting techniques

First choose the cells We need a way to pause the macro and choose the cells to be exchanged; normally you cannot do anything else in Excel while a macro is running The code for how to do this came from the Walkenbach book We use the InputBox function from Excel, NOT the one from VBA, because the one in Excel can return a value of the type we need To get that function, write Application.lnputBox Here Excel is the Application, and InputBox is one of its methods We would do the same thing to use other Excel functions in VBA code

Part of the code (explanation next) Const rangeType As Integer = 8 Dim firstCell, secondCell As Range Dim askforCell1, askforCell2, askTitle As String askforCell1 = "Select the first cell" askTitle = "Get Cell" Set firstCell = Application.InputBox( _ Prompt:=askforCell1, _ Title:=askTitle, _ Type:=rangeType)

The Declarations Const rangeType As Integer = 8 Dim firstCell, secondCell As Range Dim askforCell1, askforCell2, askTitle As String We’ve set up two variables as type Range. A Range can be one cell or a group of cells We’ve declared variables to hold the strings we’re using We declared a constant naming the code for a Range type in Excel, which our function needs

Next, Set the Strings askforCell1 = "Select the first cell by clicking on it" askTitle = "Get Cell“ These two assignment statements set the values of the strings

Call the InputBox function Set firstCell = Application.InputBox( _ Prompt:=askforCell1, _ Title:=askTitle, _ Type:=rangeType) Application.InputBox calls the function. The value goes to variable firstCell

The Line Continuation Character Set firstCell = Application.InputBox( _ Prompt:=askforCell1, _ Title:=askTitle, _ Type:=rangeType) We wanted a long statement, so we broke up into several lines (normally the end of a line is the end of a statement) Do this using a blank followed by an underscore

We used named parameters Set firstCell = Application.InputBox( _ Prompt:=askforCell1, _ Title:=askTitle, _ Type:=rangeType) The parameters are the arguments or values that we send to the function. Here we tell it the prompt to use, the title to use, and the type of the response

Next, get the second cell… askforCell2 = "Select the second cell" Set secondCell = Application.InputBox( _ Prompt:=askforCell2, _ Title:=askTitle, _ Type:=rangeType)

Then Do the Exchange temp = secondCell.Value secondCell.Value = firstCell.Value firstCell.Value = temp Variable temp was declared as type Variant. If we were sure we were working with a particular type, we could use it instead This code has the same structure as our previous example: we have an algorithm for exchanging the values of two cells