14.9.2015 1 VBA (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...

Slides:



Advertisements
Similar presentations
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Advertisements

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.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Using Macros and Visual Basic for Applications (VBA) with Excel
Tutorial 7: Developing an Excel Application
Developing an Excel Application
Tutorial 8: Developing an Excel Application
© 2002 ComputerPREP, Inc. All rights reserved. Excel 2000: Customizing Excel and Using Macros.
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
Templates and Styles Excel Advanced. Templates are pre- designed and formatted spreadsheets –They provide consistency of layout/structure –They.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
With Microsoft Excel 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2010.
Microsoft Excel 2003 Illustrated Complete with Excel Programming.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Using the Visual Basic Editor Visual Basic for Applications 1.
Adding Automated Functionality to Office Applications.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
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.
Week 1.  Kate Watson  Checked at least every other day. Queries and general comments welcome.
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.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
Microsoft Excel 2007 © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line.
11 10/10/2005 BAE Spreadsheet Macros (Visual Basic for Applications) Slides to accompany an in-class demo of spreadsheet technique: Macro Programming.
© McGraw-Hill Companies, Inc., McGraw-Hill/Irwin Extended Learning Module M Programming in Excel with VBA.
VBA for Excel. What is a spreadsheet? u An Excel spreadsheet is a set of worksheets  Each worksheets is made up of rows and columns of cells  Rows are.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
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-
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.
Microsoft Excel Macros & Excel Solver (IENG490)
Introduction on VBA Lab 05 ins.Tahani Al_dweesh. Lab Objectives Introduction Calculation with VBA Storing and Retrieving Variables in a Worksheet Using.
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
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
Lecture Excel: Macros & Pivot Tables. Macros A macro is a series of commands that are stored and can be run whenever you need to perform the task.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Committed to Shaping the Next Generation of IT Experts. Chapter 10 Customizing a Database.
UNIT 7: Using Excel in the Law Office. This Week’s Assignment You should be working on your three-part assignment Part 1 deals with the things you learned.
Visual Basic for Applications Macro Programming For Microsoft Office.
Intro to Excel - Session 7.31 Tutorial 7 - Session 7.3 Developing an Excel Application.
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.
OCC Network Drives  H:\  P:\ 
Chapter 3 The Visual Basic Editor. Important Features of the VBE Alt-F11 will open the Visual Basic Editor. The Code window is to the right, Project Explorer.
MS Access: Introduction 1Database Design. MS Access: Overview MS Access A Database Management System (DBMS) designed to create applications that organize,
Chapter 9 Macros And Visual Basic For Applications.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Introduction to Microsoft Excel Macros COE 201- Computer Proficiency.
Macros in Excel Using VBA Time Required – 5 hours.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
VBA Excel Macro 1.Create a Macro: To create a macro in Excel VBA, Create a Macro: To create a macro in Excel VBA,Create a Macro: To create a macro.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Excel Tutorial 8 Developing an Excel Application
Exploring Excel Chapter 7 Automating Repetitive Tasks: Macros and
Excel: Macros & Pivot Tables
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet?
Microsoft Office Illustrated
Microsoft Excel 2003 Illustrated Complete
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Introducing VBA Macros
Exploring Microsoft Excel
Lesson 1 - Automating Tasks
Microsoft Office Excel 2003
Microsoft Excel 2007 – Level 2
Visual Basic for Applications: Introduction
Presentation transcript:

VBA (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...

Time for demos...  Snake game  Speed game  SMS sender  Excel with movies  Sudoku solver  Etc.

What is a macro  Small computer program, that needs to run within another program.  Collection of instructions for the computer.  Automates a task for you.  What you would normally need several clicks for can now be done with one click.  Reduces the risk of errors.  Control hole computer, not just MsExcel tasks.

Recording macros  MS Office has a built-in recorder.  You can record anything you can do yourself in Excel (in theory).  You can use the recorder without knowing any VBA commands.  You can edit marcros in VBE (Visual Basic Editor)  Macros will be generated to Modules in VBE

Excercise 1  Start recording  Type your name to cell E1  Autowidth column E (doubleclick)  Stop recording  Check the macro from module1  Clear your name from cell E1 and resize the column to original width  Run the macro

Excercise 2  Start recording  Type your name to cells E1, F2 and G3  Autowidth columns E, F, and G  Stop recording  Try to shorten the code as much as possible in the module  Clear your name from cells and resize the columns to original width. Run the macro.

VBA –programming: Basic elements  If -sentence  Loop -structures  Event-Run programs  Microsoft Visual Basic Help  ’ –to comment lines in a code (apostrophe)

VBA –programming: Basic elements  If -sentence If value = true Then do this Else* do that* End If Workbook If(value=true;this;that) *Voluntary

VBA –programming: Basic elements  Enhanced If -sentence If value = 1 Then MsgBox "1.choise” ElseIf value = 2 Then MsgBox "2.choise" Else MsgBox ”No choise” End If

VBA –programming: Basic elements  Excercise III  Code a macro what reads a number from the cell A1 and multiplies it by 2 If the number is smaller than 100.  Code the program to Module and name it ExcerciseIII Sub ExcerciseIII … End Sub

Assigning macros to buttons  View – Toolbars – Form – Button  Click and draw -> Add macro

VBA –programming: Basic elements  Excercise IIIb  Assign a button to excercise III

VBA –programming: Basic elements  Enhanced If –sentence (And and Or) If value = 1 Or value = 2 Then MsgBox ”Value is 1 or 2” ElseIf value >= 2 And value <=3 Then MsgBox ”Value is between 2 and 3” Else MsgBox ”No value” End If

VBA –programming: Basic elements  Excercise IV  Code a macro what reads numbers from the cells A1 and B1.  If B1 is greater than A1 change the values. A1 gets the value of B1 and B1 gets the value of A1.  If B1 is not greater than A1, divide the numbers by 2.  The macro will be started from a button.

VBA –programming: Basic elements  Do Loop While i = 1 Do Range("A" & i).Value = i i = i + 1 Loop While i < 100  What does the loop do?

VBA –programming: Basic elements  For Next For i = 1 To 100 Range("A" & i).Value = i Next  What does the loop do?  To quit eternal loop

VBA –programming: Basic elements  Excercise V  Code a macro what types the multiplication table of number 3 to 100.  The macro will be started from a button. 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 … 100 * 3 = 300

VBA –programming: Basic elements  Excercise VI  Code a macro what types to column A, row 1 one *, to row 2 ** and finally to row 10 **********.  The macro will be started from a button. * ** *** … **********

Visual Basic Editor (VBE) Alt+F11 Place for Event-Run Macros to each Worksheet (Like Worksheet_Change ) Place for Event-Run Macros to whole Workbook (Like Workbook_Open) Place for user-defined Macros and function + global variables (Like HelloWorld)

Creating the Code. What do We Need?  Understand the MS Office object model  Access to the Visual Basic Editor VBE  Basic programming skills  Motivation

The Object Model. Why bother?  Excel (and the other Office applications) consist of a number of objects  Macro programming is actually just manipulation of these objects  When you understand the basic principle, it’s easier to learn how to program other Office programs  Press a dot after the object and you will the subobjects, properties and methods

Definition of an object  An object is a named part of a computers memory  It has certain features or properties, like some people have brown hair, others red hair etc.  It has some functionality or methods, like people are able to walk or cars able to drive  Objects can contain other objects

An example – A box Size Color Position Object properties The object might have a move method.

The Objects in Excel  The mother object is called an ”Application”  An application contains a number of workbooks.  These workbooks contain sheets.  The sheets contain ranges etc.  Application, Workbooks etc. contain many other objects, properties and methods.

Excercise VII  Try to find out who is the user of Excel (=application) at the moment.  Find a function ”at the moment”  Use MsgBox to show info to the user  Start the Macro from button

Event-run Macros  Normal Macros are run manually  Run from Buttons, Shortcuts  Event-run macros will be started automatically. They are started from an event (like Workbook_Open())  Major virus alert! If you open an application with Workbook_Open() Macro, the macro will be run.

Security levels Tools – Macros – Security If your macro doesn’t work in another computer, try to choose Medium level. Never Low!!!

Excercise VIII  Code a macro that requires a user’s name and types it to cell A1 when entering to WorkSheet 1  Use InputBox()

Excercise IX  Code a macro that automatically saves the file when closing it

Private Sub Workbook_Open() Dim Users(2) As String Dim Found as Boolean Found=False Users(0) = "Christian Brade" Users(1) = "Elvis Presley" Users(2) = "George Bush" For i = 0 To UBound(Users) If Application.UserName = Users(i) Then Found=True End If Next If Found=False Then Application.ActiveWorkbook.Close End if End Sub What does the code do?

Final advice  If you wonder how a certain operation is done, record it and look it from the code.  Place the cursor over a keyword and press F1 for help on that word.

Thank You!