Essence of programming  Branching  Repetitions.

Slides:



Advertisements
Similar presentations
© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 2Managing and Integrating Data and the Excel Environment Chapter 7Automating Repetitive Tasks.
Advertisements

Microsoft Office XP Microsoft Excel
 Use the Left and Right arrow keys or the Page Up and Page Down keys to move between the pages. You can also click on the pages to move forward.  To.
Microsoft Excel 2003 To start Excel, click the start button. A slightly different procedure might be required for computers on a network. If you need assistance,
WORKING SMART Crystal M. Thomas Henrico County DSS (804) POSSESS Central Region Member October 31, 2007.
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
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.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Using Microsoft Office Excel 2007
Advanced Microsoft Word Hosted by Jared Hoffman Topics Keyboard Shortcuts Customizing Toolbars and Menus Auto Format & Auto Correct Tabs Inserting Pictures.
Microsoft Excel 2003 Illustrated Complete A Worksheet Formatting.
Customizing Word Microsoft Office Word 2007 Illustrated Complete.
Lecture 1 Introduction to Excel OVERVIEW Introduction Basics of Cells Modifying Columns and Rows Formatting Cells Saving Working with Formulas Basics.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Excel Lesson 14 Creating and Using Macros Microsoft Office 2010 Advanced Cable / Morrison 1.
Business Computer Information Systems 1A Test 2: Word Basics, Basic Editing, and Formatting Text Lessons 1, 2, and 5 Microsoft Office XP Test 2 – REVIEW.
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.
1 Macros Presented by Maria G. Martinez. 2 What's a macro?  Macro - set of computer instructions that you can record and associate with a shortcut key.
© 2002 ComputerPREP, Inc. All rights reserved. Word 2000: Forms, Merges, and Macros.
Laboratory Exercise # 13 – Font and Number Format Styles Office Productivity Tools 1 Laboratory Exercise # 13 Font and Number Format Styles Objectives:
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
© 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.
EXCEL 2007 MACROS.  TOM FARRAR OF CNEXTWAVE INC.  GOAL : LEARNING ABOUT EXCEL MACROS.  PRESENTATION INCLUDES DISCUSSION AND DEMONSTRATION.  QUESTIONS.
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-
Microsoft Excel Macros & Excel Solver (IENG490)
Microsoft Excel By: Dr. K.V. Vishwanath Professor, Dept. of C.S.E,
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
Microsoft Office Illustrated Introductory, Premium Edition A Worksheet Formatting.
Microsoft Office XP Illustrated Introductory, Enhanced A Worksheet Formatting.
Microsoft Excel Spreadsheet Review. Templates  Templates can be produced for the following elements:  Text and Graphics  Formatting Information – Layouts,
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Microsoft Access Lesson 1 Lexington Technology Center February 11, 2003 Bob Herring On the Web at
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
MENU BAR This is the traditional windows style drop-down menu. Clicking on the desired command would tell Word to execute that command. Some commands.
Microsoft Visual Basic 2005 ENRICHMENT CHAPTER Visual Studio Tools for Office.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic for Applications Macro Programming For Microsoft Office.
Intro to Excel - Session 7.31 Tutorial 7 - Session 7.3 Developing an Excel Application.
Chapter 9 Macros And Visual Basic For Applications.
1. 2 Word Processing Word Processing is writing words and sentences on the computer. It is easy to change or move text in a word document. People use.
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.
1 Excel Lesson 1 Microsoft Excel Basics Microsoft Office 2010 Pasewark & Pasewark.
Introduction to Microsoft Excel Macros COE 201- Computer Proficiency.
Excel Spreadsheets Formatting and Functions ICS100 – Spring 2007 D. Pai.
Chapter 2: Excel Basics and Formatting Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Microsoft Excel ( XP-2003). Return to Table of Contents Table of Contents 1_ Introduction to ExcelIntroduction to Excel 2_ Overview of the Excel.
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.
Computer/LMS Access To log onto one of these computers: Enter your Username, for example: 2014BNS099 followed So a complete login.
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.
Word processing is the software package that enables you to create,edit, print and save documents for future retrieval reference. creating a document.
Chapter 11 Enhancing an Online Form and Using Macros Microsoft Word 2013.
Excel Tutorial 8 Developing an Excel Application
Microsoft Excel.
Microsoft Excel.
Lessons Vocabulary Excel 2016.
Microsoft Excel 2003 Illustrated Complete
Microsoft Excel All editions of Microsoft office.
Exploring Microsoft Excel
Lesson 1 - Automating Tasks
Microsoft Excel 2007 – Level 2
Day 2: Fill Data Automatically in Worksheet Cells
Presentation transcript:

Essence of programming  Branching  Repetitions

Flowchart for the MinValue function

Code for the MinValue function Function MinValue(n1 As Single, n2 As Single) As Single If n1 <= n2 Then MinValue = n1 Else MinValue = n2 End If End Function

Flowchart for the testfunction

Code for the testfunction Function testfunction(P1 As Single, P2 As Single) As Integer If P1 <= 6 Then testfunction = 90 ElseIf P2 > 11 Then testfunction = 30 Else testfunction = 40 End If End Function

Flowchart for the simple factorial function

Code for the simple factorial function Function factorial(n As Integer) As Long Dim i As Integer factorial = 1 For i = 1 to n factorial = factorial * i Next i End Function

Steps of calculating 5! Begin: factorial = 1 Step 1: factorial = 1 * 1 = 1 Step 2: factorial = 2 * 1 = 2 Step 3: factorial = 3 * 2 = 6 Step 4: factorial = 4 * 6 = 24 Step 5: factorial = 5 * 24 = 120 End

Flowchart for the recursive factorial function

Code for the recursive factorial function Function factorial(n As Integer) As Long If n > 0 Then factorial = n * factorial(n – 1) Else factorial = 1 End If End Function

Steps of calculation 5! (recursive) Begin Step 1: factorial(5) = 5 * factorial(4) Step 2: factorial(4) = 4 * factorial(3) Step 3: factorial(3) = 3 * factorial(2) Step 4: factorial(2) = 2 * factorial(1) Step 5: factorial(1) = 1 * factorial(0) Step 6: factorial(0) = 1 End

Calculating number e as the sum of a series

Use already defined factorial function

Function code Function handmadeE() As Double Dim i As Integer handmadeE = 0 For i = 0 to 10 handmadeE = handmadeE + 1/ factorial(i) Next i End Function

Flowchart for the fibonacci function

Calculating the sum of a series Calculate the sum of the first 100 elements of the series

Calculating the sum of a series: flowchart. Parameter P does not depend on index n; therefore, it can be put as the factor.

Subroutine code to print the result in the immediate window Sub calculateseries(P As Single) Dim Sum As Double Dim n As Integer Sum = 0 For n = 1 To 100 Sum = Sum + 1/(1 + n)^(1/n) Next n Sum = Sum * P debug.print “Sum = “; Sum End Sub

Function code to display the result on the worksheet Function calculatesrs(P As Single) As Double Dim n As Integer calculatesrs = 0 For n = 1 To 100 calculatesrs = calculatesrs + 1/(1 + n)^(1/n) Next n calculatesrs = calculatesrs * P End Function

Exploring Microsoft Office Visual Basic in Macros

VBA, Microsoft Office, Macro VBA is the key to customize Word and Excel Macro –A sequence of instructions executed as one –automate a procedure frequently performed –Can be invoked by a single command/action –It’s essentially a program Can be programmed Better yet, can be recorded…

Macros: In a Labsession you have already seen how to write a subroutine (Macro) using the VBA editor. Alternatively you can also create them with the Macro recorder. In this way you do not need to know any VBA commands. Recording a Macro: 1) open a worksheet 2) select Tools  Macro  Record New Macro   the record Macro dialog box opens up 3) enter Macro Name, e.g. “SumA1toA30“ - not all names are allowed, such as function names, special signs in the name as !,?, blank,... are also not possible 4) enter a letter for the shortcut key, e.g. “s“ 5) store the macro somewhere, e.g. “This workbook“ 6) fill in the decription box, e.g. “sum up the cells A1:A30“ 7) Ok , the recording is on. Now all actions you carry out on the worksheet will be recorded and its code will be produced.

8) For example: Carry out an action which sums up the cells A1:A30 - select the adjacent cell and write: “=Sum(A1:A30)“ - alternatively in the window on the worksheet select Stop Recording  - if that window is now invisible, you can make it appear by selecting Edit  Toolbars  Stop Recording  - select a cell in your worksheet different from column A - write: “ The sum of the cells A1:A30 is: “ 9) - select Tools  Macro  Stop Recording  - the effect of this is that in the cell in which you wrote “=Sum(A1:A30)“ this sum will be displayed · if a cell is empty its value contributes zero to the sum · you can now change the content of A1:A30 and the sum will be updated automatically

Record Macros Tools Menu Macro Command –Record Macro

Recording a Macro In the Tools menu choose Macros – Record new macro Give a name to the macro Start recording Do some actions Stop recording Edit the macro Use the macro

Viewing the code: - Let‘s see what we have just produced: - Select Tools  Macro  Macros   a window called Macros opens up - the window “Macro name“ shows the name of the Macro - in case you have many Macros: select Options  to see the details of it (in case you do not remember) - Select Edit   the code we have just produced will show up - The recording has produced a VB code, which alternatively we could have programmed by hand:

Sub SumA1toA30() ' ' SumA1toA30 Macro ' sum up the cells A1:A30 ' ' Keyboard Shortcut: Ctrl+s ' Range("F12").Select ActiveCell.FormulaR1C1 = "The sum of the cells A1:A30 is:" Range("I12").Select ActiveCell.FormulaR1C1 = "=SUM(R[-11]C[-8]:R[18]C[-8])" End Sub

Activating the Macro: i) Select Tools  Macro  Macros   a window called Macros opens up the macro‘s name appears in the window “Macro name:“ · in case you have more than one, select the one you want Select Run   what you have recorded before will be executed now ii) Use the shortcut: - our example just: Ctl + s iii) If you were editing the code: Select    a window called Macros opens up  i) iv) Using customized buttons or other objects

Running a Macro Tools  Macro  Run –or use shortcut Attach a macro to a button on the Forms toolbar : easy –right Click  Assign Macro Attach a macro to a (command) button on the Control Toolbox toolbar : need VB editor –Call macro name from click method of the button

Run Macros  Tools Menu  Macro Command Macros Keyboard Tool buttons

Example: We calculate once more - put 1 into cell A1: - select Edit  Fill  Series   a window called Series opens up - Fill in: Series:  Column Type:  Linear Step value: 1 Stop value: 30 - Ok  - first you have to fill in: 1  A1, 2  A2, 3  A  A30 - you can do this by hand, but the faster way is to use “Series“: - activate the Macro  The sum of the cells A1:A30 is 465

Macro doing repetitive steps KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP KIMEP

Macro code Sub kimep200() ' ' kimep200 Macro ' Macro recorded 2/2/2003 by dvm ' Dim counter As Integer For counter = 1 To 200 Selection.TypeText Text:="KIMEP " Next counter End Sub

Formatting macro code Sub kformat() ' ' kformat Macro ' Macro recorded 2/4/2003 by dvm ' Dim counter As Integer For counter = 1 To 200 Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend Selection.Font.Color = wdColorRed Selection.Font.Bold = wdToggle Selection.Font.Italic = wdToggle If Selection.Font.Underline = wdUnderlineNone Then Selection.Font.Underline = wdUnderlineSingle Else Selection.Font.Underline = wdUnderlineNone End If Selection.MoveRight Unit:=wdCharacter, Count:=3 Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend Selection.Font.Color = wdColorBlue Selection.MoveRight Unit:=wdCharacter, Count:=3 Next counter End Sub

Use Controls on a Worksheet Two types of Controls –Forms toolbar: Simple, compatible with Excel earlier version –Control Toolbox toolbar: complex, can do everything Forms controls do, and much more (We’ll focus on this) –buttons, checkboxes, etc. Get Forms toolbar: View  Toolbars  Forms Get Control Toolbox toolbar: View  Toolbars  Control Toolbox Focus on Command Button in Control Toolbox –design mode

A control(e.g. a Command Button) as an object A control is an object –Objects have identity, properties, methods Identity (Name): –Is also a property (called Name), but each control’s name has to be unique on the worksheet Each control has properties associated with it –each property has a value –e.g. Font = Arial ; Height = 24 –See the list of properties by right clicking on it (in design mode) Each control also has methods associated with it –e.g. Click(what will happen if clicking on it), DblClick –double click on the control in design mode to see list of methods(upper right drop down list) in VB editor

iv) Changing the button design: change the size of the button: - select the right mouse button (moving first over the button)  select Format Control   Alignment   Automatic size  Ok  similarly you can change the writing direction, the text fonts, the text and button size, the margins of the button, the colour, the status of the protection, etc. attach a better text to the button: - select the right mouse button (moving first over the button)  a variety of commands opens up: Cut, Copy, Paste,...  select Edit text   type a meaningfull text onto the button, e.g. Sum A1 to A30

You can also assign Macros to other objects: · the  symbol from the forms toolbar · a text label Aa on the forms toolbar · other symbols from the forms toolbar · a picture you have imported before such as (Select Insert  Picture  From File or Clip Art  choose a picture) · etc.