VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.

Slides:



Advertisements
Similar presentations
Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In.
Advertisements

Integrated Business Applications with Databases (D3) Jenny Pedler
1B40: Data analysis 1 Objects Visual Basic is an (OO) object-oriented language. Performing a task in Visual Basic (VB) or.
情報基礎 A Lecture 10 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information.
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.
EXCEL VBA 101 Current Status Waiting Using Excel/VBA to Speed-up Daily Data Processing An Introduction to VBA Yongjun Chen Click Me to Start …
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.
Exploring Microsoft Excel 2002 Chapter 9 Chapter 9 A Professional Application: VBA and Date Functions By Robert T. Grauer Maryann Barber Exploring Microsoft.
Week 5.  Recap – For Each..Next  Personal Macro File  Immediate window  Object Variables Working with worksheets and workbooks  For Loops.
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
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.
VBA Programming Part One. Our Dartmouth Legacy 10 INPUT "What is your name: "; U$ 20 PRINT "Hello "; U$ 25 REM 30 INPUT "How many stars do you want: ";
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
 Excel – Basic Elements  Using Macros  Excel VBA Basics  Excel VBA Advanced.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...
© McGraw-Hill Companies, Inc., McGraw-Hill/Irwin Extended Learning Module M Programming in Excel with VBA.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
The animation is already done for you; just copy and paste the slide into your existing presentation. Dony Pranadiyanta, ST.
Creating Macros Using VBA. Assigning a Macro to a Button Display the Forms toolbar. Click the Button icon. Click and drag the mouse pointer to specify.
Introduction on VBA Lab 05 ins.Tahani Al_dweesh. Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using.
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)
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Microsoft Access Using Visual Basic Routines. Visual Basic Datatypes Boolean Byte Currency Date Double Integer Long Object Single String Variant Hyperlink.
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.
OV Copyright © 2011 Element K Content LLC. All rights reserved.  Insert Text  Format Text  Sort Data  Duplicate Data  Generate a Report Formatting.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
Chapter 9 Macros And Visual Basic For Applications.
Week 6.  Assessment 2  Do Loops  Custom Functions.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
ME 142 Engineering Computation I Using Subroutines Effectively.
Lab 2 Introduction to VBA (II) ► Lab 1 revisited - Sub & Function procedures - Data types and variable declaration ► Recording.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Chapter 11: Introduction to VBA Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
A Macro to Exchange the Values of Arbitrary Cells.
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.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Key Concepts 2 Simple VB Procedures Simple VB Applications - take with Other Examples - time permitting.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
Financial Information Management VB, VBA, VS, VSTO & VBE: Putting it all together Source: Excel VBA Programming by John Walkenbach.
© Stefano Grazioli - Ask for permission for using/quoting: Source: Excel VBA Programming by John Walkenbach.
Macros in Excel Using VBA Time Required – 5 hours.
VBA Programming for Excel Review Excel Objects Excel Methods Identifying Specific Cells Review Functions for Excel Custom Menus.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Developing an Excel Applications
IE 8580 Module 4: DIY Monte Carlo Simulation
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
VBA programming for Excel
Spreadsheet-Based Decision Support Systems
Excel VBA Day 3 of 3 Tom Vorves.
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
EXCEL Creating An Array In VBA
Activate a range and manipulating activated range
VBA Programming for Excel
Presentation transcript:

VBA Programming Session #2

Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next

Variables  Just like in math, variables: Have a name Have a value Value can be changed  Examples: x = 1 : debug.print x x = 2 : debug.print x

Procedures  Subroutines (Subs) Can do things but not return values. Subs without arguments can be called from the Excel macro toolbar.  Functions Functions return values in VBA with the form: FunctionName = SomeValue Functions can be inserted into Excel sheets from the Insert|Function menu.

If…Then  If…Then has two basic forms: Statement If (x = 1) Then Debug.Print "Yes" Code Block If (x = 1) Then y = 1 Else y = -1 End If

For…Next  Loops through a range of values and executes the statements in the block. For x = 1 To 10 Debug.Print x Next Etc.

New Stuff

VBA Variable Types  The most commonly used VBA types are: Boolean (True or False) Numeric Types: Integer, Double, Currency Date String Variant (holds almost anything) Object Types: Ranges, Worksheets, etc.

Objects  A combination of code and data that can be treated as a unit, for example, a control, form, or application component. Each object is defined by a class. (MS)  Examples: Workbooks Worksheets Charts Ranges

Declaring Variables  Declaring means: Telling VBA that you are going to use it Defining the variable's type  Declare variables using the Dim statement Dim MyName as String Dim MyBirthday as Date Dim BigMoney as Currency Dim wks as Worksheet

Assigning Values to Variables  Most variables can simple be assigned like this: X = 5 MyName = "Bob"  Objects need to be assigned using Set Set MyDataSheet = ActiveSheet Set MyChart = ActiveChart

Forgot to set?

Range Object  Represents a cell, a row, a column, a selection of cells containing one or more contiguous blocks of cells, or a 3-D range. (MS)

Understanding Ranges

Ranges: Writing Code Sub SimplestHello() Range("A1") = "Hello" Range("A2", "B4") = "Goodbye" End Sub

Ranges: Using a Range Variable Sub HelloRange() Dim a1 As Range Set a1 = Range("A1") a1 = "Hello world!" End Sub

Offsets: If only there was just "A1"

Combining it all... Data Table

The Macro Recorder: Part II

Specific -> Generic  Instead of... Range("A1") Sheets("Sheet1") Charts("Chart1")  Use Selection or ActiveCell ActiveSheet ActiveChart

Collection Object  Most of the VBA objects you use are parts of collections. For example: Charts: a collection of Chart objects Worksheets: a collection of Worksheet objects  Collections have the following: Count: number of items in the collection Add: add an item Item: get the value of an item Remove: delete item

Collection: Example Sub AddWorksheet() Dim wks As Worksheet Set wks = Worksheets.Add MsgBox "Added: " + wks.Name End Sub

Looping Through Collections  Collections are handy to use because we can easily look at each item in the collection using a "For Each" loop.  For example: Sub ListWorksheets() Dim wks As Worksheet For Each wks In ActiveWorkbook.Worksheets MsgBox wks.Name Next End Sub

Collections: Exercise