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.

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

Lesson 1 Review Part 3 Microsoft Office The ___ allow you to move up/down or right/left in a worksheet. Scroll bars.
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.
Using Macros and Visual Basic for Applications (VBA) with Excel
Developing an Excel Application
Tutorial 8: Developing an Excel Application
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
Programming in Visual Basic
String Variables Visual Basic for Applications 4.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
FIRST COURSE Excel Lecture. XP 2 Introducing Excel Microsoft Office Excel 2007 (or Excel) is a computer program used to enter, analyze, and present quantitative.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Excel Lesson 14 Creating and Using Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
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.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Object Variables Visual Basic for Applications 3.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
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.
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.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
 Introduction to MS-Excel Introduction to MS-Excel  Entering data in EXCEL Entering data in EXCEL  Formulas & Functions in EXCEL Formulas & Functions.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
OV Copyright © 2011 Element K Content LLC. All rights reserved.  Create a Macro with the Macro Recorder  Edit a Macro  Debug a Macro  Customize.
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
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Working with Numeric Variables (Unit 6) Visual Basic for Applications.
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’
1. Go to: Start-Programs-Microsoft Office Excel 2007 Click Microsoft Office Excel 2007 to start Excel and display a new blank workbook titled Book1 in.
® Microsoft Office 2010 Excel Tutorial 1: Getting Started with Excel.
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.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
# 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 with Microsoft Visual Basic th Edition
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Calculator Program Explained by Arafa Hamed. First Designing The Interface Ask yourself how many places are there that will be used to input numbers?
Microsoft Excel Microsoft Excel 2013 is a spreadsheet application in the Microsoft Office Suite. A spreadsheet is an accounting program for the.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Macros in Excel Using VBA Time Required – 5 hours.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
COMPUTER LITERACY NOTES MS-EXCEL. SPREADSHEETS A spreadsheet is a computer equivalent of a paper ledger sheet. Excel allows you to create spreadsheets.
1 Excel Lesson 7 Working with Multiple Worksheets and Workbooks Microsoft Office 2013 Introductory.
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.
Excel Tutorial 8 Developing an Excel Application
Excel Lesson 7 Working with Multiple Worksheets and Workbooks
VBA - Excel VBA is Visual Basic for Applications
MS-EXCEL SUMMARY.
After completing this lesson, you will be able to:
Microsoft Excel A Spreadsheet Program.
Developing an Excel Application
EXCEL Introduction.
Microsoft Office Illustrated
Microsoft Excel Unit 1.
Microsoft Excel 2003 Illustrated Complete
MS-Office It is a Software Package It contains some programs like
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Microsoft Visual Basic 2005 BASICS
Data Types List Box Combo Box Checkbox Option Box Visual Basic 6.0
Tutorial 11 Using and Writing Visual Basic for Applications Code
Microsoft Excel 2007 – Level 2
Presentation transcript:

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 then click on the Excel Options button

Open the Visual Basic Editor in Excel 2007 When the Excel Options window appears, click on the Popular option on the left. Select the option called "Show Developer tab in the Ribbon". Then click on the OK button.

Open the Visual Basic Editor in Excel 2007 Select the Developer tab from the toolbar at the top of the screen. Then click on the Visual Basic option in the Code group.

Open the Visual Basic Editor in Excel 2007 Now the Microsoft Visual Basic editor should appear and you can view your VBA code.

Variables and Data Types To use some values in code, you must first create them In the world of computer programming, a variable is a value you ask the computer to temporarily store in its memory while the program is running.

Declaring a Variable In order to reserve that storage area, you have to let the computer know. Letting the computer know is referred to as declaring the variable. To declare a variable, you start with the Dim word. Dim VariableName As DataType

Declaring a Variable There are rules you should follow when naming your variables: ▫The name of a variable must begin with a letter or an underscore ▫After starting with a letter or an underscore, the name can be made of letters, underscores, and digits in any order ▫The name of a variable cannot have a period ▫The name of a variable can have up to 255 characters. ▫The name of a variable must be unique in the area where it is used

Data Types Byte: ▫To declare a variable that would hold natural numbers that range from 0 to 255, use the Byte data type. Here is an example: Sub Exercise() Dim Value As Byte Value = 246 End Sub

Data Types Integer: ▫To declare a variable that would hold a number that ranges from to 32767, use the Integer data type Long: ▫A long integer is a number that can be used for a variable involving greater numbers than integers. To declare a variable that would hold such a large number, use the Long data type Double: ▫If you want to use a decimal number that requires a good deal of precision, declare a variable using the Double data type.

Data Types String: ▫A string is a character or a combination of characters that constitute text of any kind and almost any length. To declare a string variable, use the String data type. Here is an example: Sub Exercise() Dim CountryName As String CountryName = “Lebanon" End Sub

VBA in Excel/ Worksheets In Microsoft Excel, a spreadsheet is called a worksheet A workbook is a series of worksheets that are treated as a group. A worksheet is an object of type Worksheet. Another name for the collection that contains the worksheets is called Sheets. In most cases, you can use either of these two collections. Each worksheet is an object of type Worksheet.

Referencing a Worksheet worksheets of a document are part of the workbook that is opened the Workbook class is equipped with a property named Worksheets or Sheets. Therefore, after identifying the workbook, use the period operator to access the Worksheets or the Sheets property. Each worksheet can be located based on an indexed property named Item Sub Exercise() Workbooks.Item(1).Sheets.Item(2) End Sub

Referencing a Worksheet Other ways to reference a worksheet: ▫Omitting Item(): Sub Exercise() Workbooks.Item(1).Worksheets(2) End Sub ▫Using the sheet name: Sub Exercise() Workbooks.Item(1).Sheets.Item("Sheet3") End Sub

The Cells of a Worksheet Cell Referencing You can identify a cell using the Range object. To do this, in the parentheses of the Range object, pass a string that contains the name of the cell. Here is an example that refers to the cell located as D6: Sub Exercise() Workbooks.Item(1).Worksheets.Item("Sheet1").Range(" D6") End Sub

Cell Referencing To get a reference to a cell, declare a variable of type Range. To initialize the variable, identify the cell and assign it to the variable using the Set operator. Here is an example: Sub Exercise() Dim Cell As Range Set Cell = Workbooks.Item(1).Worksheets.Item("Sheet1").Range("D6") End Sub Multi-Cell Ranges: Sub Exercise() Range("D2:B5, F8:I14") End Sub

Cell Contents To set a Cell’s contents to a certain value, just use the ‘=‘ operator: Sub Example() Sheets.Item(1).Range("A1") = “Hello" End Sub To insert a formula, just ad an ‘=‘ before the formula’s name: Sub Example() Sheets.Item(1).Range(“B1") = "=sum(A1:A10)" End Sub

Cell Background Color To change a cell’s backgroud color, use the interior.color property: Sub Example() Sheets.Item(1).Range("A1").Interior.Color = RGB(255,0,0) End Sub