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

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

Using VB with MS Applications R. Juhl, Delta College.
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 …
Essence of programming  Branching  Repetitions.
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
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
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.
Microsoft Office Excel 2013 Core Microsoft Office Excel 2013 Core Courseware # 3253 Lesson 8: Macros, Importing and Exporting Data.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Excel and VBA Creating an Excel Application
Using the Visual Basic Editor Visual Basic for Applications 1.
Lecture Excel: Multiple Worksheets. Workbook and Worksheets Multiple worksheets in a single workbook. When saved, only a single workbook (XLS) is saved.
Macros in Excel Intro to lab 1. Macroinstructions Macro is recorded in VBA module sequence of Excel operations Macros can automate tasks in Excel Macro.
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.
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.
IE 212: Computational Methods for Industrial Engineering
With Microsoft Excel 2007 Comprehensive 1e© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Excel 2007 Comprehensive.
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.
VBA (Visual Basic for Applications) What is Excel, just a spreadsheet? Time for Demos...
Adapting a ready made PowerPoint quiz 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.
ChE 117 Motivation Lots of Tools Can’t always use “Canned Programs”
1 CA201 Word Application Increasing Efficiency Week # 13 By Tariq Ibn Aziz Dammam Community college.
Microsoft Excel Macros & Excel Solver (IENG490)
The animation is already done for you; just copy and paste the slide into your existing presentation. Dony Pranadiyanta, ST.
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
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.
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.
Intro to Excel - Session 7.31 Tutorial 7 - Session 7.3 Developing an Excel Application.
Excel VBA TOM VORVES DO THE ACTIVITY EXERCISE ON PAGE 53 IF YOU WISH ElaineNikky JennArrinMylinh Matt Move Row.
OCC Network Drives  H:\  P:\ 
Using Visual Basic for Applications in Microsoft Project Sean Vogel.
Chapter 9 Macros And Visual Basic For Applications.
Count and add list of numbers From user input and from file.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
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.
Durham County Government A Brief Intro. To Microsoft Excel Macros: Making Life A Little Easier Presented By: Kevin Etheridge, Budget.
Excel Macros 1 Macros or, How to Automate Part of Your Spreadsheet or Worksheet.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Chapter 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
1 CA202 Spreadsheet Application Automating Repetitive Tasks with Macros Lecture # 12 Dammam Community College.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
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
VBA - Excel VBA is Visual Basic for Applications
Excel: Macros & Pivot Tables
Microsoft Office Illustrated
Excel VBA Day 3 of 3 Tom Vorves.
Microsoft Excel 2003 Illustrated Complete
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Introducing VBA Macros
Python I/O.
أ.إسراء الطريقي أ. هاله الشملان , 102 تقن , المعمل الخامس
Lesson 1 - Automating Tasks
Microsoft Office Excel 2003
Presentation transcript:

Excel and Visual Basic

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

Exchange data with.csv file.csv file is “comma separated value” file..csv file is plain text file..csv file can only save one worksheet. For multiple worksheets in a workbook, you need to use several.csv files. You need do some editing to remove titles or labels before you use VB to read it.

Example

Code Private Sub Command1_Click() Dim payment(1 To 50) As Single, commission(1 To 50) As Single Dim index As Integer Open App.Path & "\commission.csv" For Input As #1 index = 1 Do While Not EOF(1) Input #1, payment(index), commission(index) index = index + 1 Loop Close #1 End Sub

Using VB in Excel Excel uses a special version of Visual Basic, which is called Visual Basic for Application (VBA) User can use VBA to create customized Excel function (VBA procedures), which can be much more powerful than the build-in Excel functions.

Security issues VBA procedures in Excel is also called Macros. Macros are two-edged swords. Macros viruses are malicious VBA procedures.

Setting the security level

Security Level High (default) Medium. We should set the security to this level to run our VBA procedure. Low (not recommended)

Medium level security If you set the security level to medium, every time you open a Excel file containing macro, Excel will ask you if you want to enable the macro.

Example

VBA function code Function Comm(Sales_V As Single) as Single If Sales_V =500 and Sales_V =1000 and Sales_V =200 and Sales_V =5000 Then Comm=Sales_V*0.15 End If End Function

Edit VBA program in Excel

Insert modules The VBA procedure should be defined in a module. If you don‘t have an existing module, create one by using the insert->Module menu.

Write code in visual basic editor

Final result

Passing a range of cells to VBA procedure The previous example shows how to pass one value to a VBA function. However, in Excel it is possible to pass several cells to a build-in function. E.g. SUM(a5:a7) We call a5:a7 is a range. User defined VBA function can also get a range of cells as input.

Data type: range VBA defines a specific data type: range Range is just like a object: it has properties. Important properties of range Count : How many cells in the range Value : Value of the cell (it works when range refers to only one cell)

Accessing individual cells We use a special form of For…Next loop to access each cell in a range Function meanvalue(InputRange As Range) As Single Dim cl As Range For Each cl In InputRange ‘some code here to get the value ‘cl.value keep the value of the cell Next cl End Function Red word are key word in Visual Basic

Program task Define a VBA function that calculate the average of a range of cells. We define the function’s name as meanvalue.

code Function meanvalue(InputRange As Range) As Single Dim cl As Range ‘cl is used to get individual cell Dim index As Integer, sum As Long index = 1 ReDim inputarray(1 To InputRange.Count) As Single For Each cl In InputRange inputarray(index) = cl.Value ‘save cell value into array index = index + 1 Next cl sum = 0 For i = 1 To InputRange.Count sum = sum + inputarray(i) Next meanvalue = sum / InputRange.Count End Function

Use your defined VBA function