12. Visual Basic If Statements and Do Loops. Open 12b-datastart.xlsm.

Slides:



Advertisements
Similar presentations
1 Alt + ‘ Ctrl + Shift + ~ Ctrl + Shift + $ Ctrl + Shift + % Ctrl + Shift + ! Ctrl + Shift + & Ctrl + Shift + _ Ctrl + b Ctrl + i Ctrl + u Ctrl + 9 Ctrl.
Advertisements

Excel 101 Excel 101 By Raji Aboulhosn. Using keyboard shortcuts To copy, press Ctrl+C. To cut, press Ctrl+X. To paste, press Ctrl+V. Using the mouse To.
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
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
© 2002 ComputerPREP, Inc. All rights reserved. Excel 2000: Customizing Excel and Using Macros.
Macros Excel built-in functions are great but limited Macros are a means for the user to define new functions A macro is a single command that automates.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Excel Project 1 Creating a Worksheet and an Embedded Chart.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Adding Automated Functionality to Office Applications.
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)
Spreadsheet revision By Charlotte Neal. Spreadsheet oA Spreadsheet is a computer program that works like a paper worksheet. You can put words and numbers.
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.
Give it, Live it, Doing the Pivot Louise Cape, James Colombo, Katie McDonald, Tammy Rowland.
Spreadsheet. Objectives Create a new blank workbook. Create a new blank workbook. Identify user interface elements that you can use to accomplish basic.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
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.
11. Visual Basic Macro Recorder. Open Excel Click File -> Save As.
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.
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...
11 10/10/2005 BAE Spreadsheet Macros (Visual Basic for Applications) Slides to accompany an in-class demo of spreadsheet technique: Macro Programming.
© 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-
European Computer Driving Licence Syllabus version 5.0 Module 4 – Spreadsheets Chapter 22 – Functions Pass ECDL5 for Office 2007 Module 4 Spreadsheets.
Introduction to VBA MGMI Aug What is VBA? VBA = Visual Basic for Application Excel’s powerful built-in programming language An event-driven.
Excel Formulas. File -> Open -> 02b-datastart.xlsx.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Excel Project 1 Creating a Worksheet and an Embedded Chart.
IC 3 BASICS, Internet and Computing Core Certification Key Applications Lesson 10 Creating and Formatting an Excel Worksheet.
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
EXCEL TIPS. Moving around the spreadsheet quickly Home key: moves the active cell highlight to column A without changing rows. Ctrl + Home keys: moves.
MICROSOFT WORD Options like New, save, Close, Selecting Text How to change text using Bold, Italics, Underline How to change Font, Font Size, Color Justification.
Chapter 12: Recording Macros Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Just as there are many human languages, there are many computer programming languages that can be used to develop software. Some are named after people,
Visual Basic for Applications Macro Programming For Microsoft Office.
Excel CREATING A WORKSHEET AND CHART. Personal Budget Worksheet We will create a personal budget worksheet that shows you income each month and your expenses.
MS Excel – Macros using VBA By :- Amit Dhingra. CSC Proprietary 06/09/2004 Page: 2 Agenda Macro Security Recording a Macro Running a Macro Introduction.
Intro to Excel - Session 7.31 Tutorial 7 - Session 7.3 Developing an Excel Application.
WORKING WITH MACROS Session Taught by Jennifer Garbett and Zahra Khushbakht.
Microsoft Publisher 2010 Chapter 7 Advanced Formatting and Merging Publications with Data.
# 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute.
Chapter 1 Creating a Worksheet and a Chart
Microsoft Access 2010 Chapter 8 Advanced Form Techniques.
Chapter 9 Macros And Visual Basic For Applications.
Microsoft Word Tutorial By Diana Rengifo Sean Choi Robert Olson.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
05. Data from Bloomberg Add-in. Launch Bloomberg and Press GO.
Basic Microsoft Excel & Completing Reading Challenge Card.
Chapter 2: Excel Basics and Formatting Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Introduction to Macros in Excel Franco Musso. ActiveCell.Offset.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
Microsoft Excel Prepared by the Academic Faculty Members of IT.
1 CA202 Spreadsheet Application Automating Repetitive Tasks with Macros Lecture # 12 Dammam Community College.
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.
Excel Chapter 1 Creating a Worksheet and an Embedded Chart
Software Development Languages and Environments. Computer Languages Just as there are many human languages, there are many computer programming languages.
Excel Tutorial 8 Developing an Excel Application
June 17, 2009 Office 2007 Tips & Tricks.
Shelly Cashman: Microsoft Excel 2016
European Computer Driving Licence
Advanced Microsoft Excel
أ.إسراء الطريقي أ. هاله الشملان , 102 تقن , المعمل الخامس
Lesson 1 - Automating Tasks
Microsoft Excel 2007 – Level 2
Day 2: Fill Data Automatically in Worksheet Cells
Presentation transcript:

12. Visual Basic If Statements and Do Loops

Open 12b-datastart.xlsm

If statements We have seen how to use IF statements in formulas in Excel IF statements can also be used in Visual Basic but they have a different format The basic format is: If a cell has a value greater than 10 Then Make the font bold ElseIf cell has a value less than 10 Then Make the font italic EndIf Begin with Macro Recorder and then edit

Select Sheet3 and cell D2

Click Record Macro

TypeMacro name: HighlightCells Shortcut key: Ctrl+j

Click OK

Right-click with mouse and select Format Cells

Select Bold Font and click OK

Click Stop Recording

Click Visual Basic

Select Module 2

Excel produces code for all aspects of the font We are only concerned with.FontStyle = “Bold”

Just keep the following lines

We only want Excel to do this when the cell value is greater than 10

If ActiveCell.Value > 10 Then … Endif

Save and Close

Click on cell D3 and press Ctrl+j to run macro

Excel should evaluate the cell and give it a bold font as it has a value above 10

Click Visual Basic

Want to add another condition that if value is less than 10 give it Italic font

ElseIf ActiveCell.Value < 10 Then

Repeat FontStyle code, but change it to Italic

Save and Close

Click Cell E2 and press Ctrl + j to run macro

Font should become Italic

Challenge Edit the Visual Basic code so that there are three conditions If ActiveCell.Value > 20 Then – Font style should be “Bold” ElseIf ActiveCell.Value > 15 Then – Font style should be “Italic” ElseIf ActiveCell.Value < 15 Then – Font style should be “Bold Italic” EndIf

Do Loops We can get Excel to do something repeatedly by setting up a Do … Loop Until … Rather than evaluating one cell at a time we may want to work through all the cells in a row We can tell Excel to evaluate the cell and move to the next cell We then get Excel to repeat this until the next cell is blank

Click Visual Basic

After the If statement we want to select the cell in the next column

Type ActiveCell.Offset(0,1).Select

Click Save and Close

Select cell C4 and press Ctrl+j to run macro

Excel evaluates the cell and moves to the next column

Click Visual Basic

We want Excel to Do this repeatedly until the next cell is empty

Type Do before the If statement

Type Loop Until ActiveCell.Value = “”

Save and Close

Select cell C5 and press Ctrl+j to run macro

Excel repeats the command and evaluates each cell within the row

Excel stops running the command when it finds an empty cell

Challenge Edit the Visual Basic code so that once the end of the row is reached, Excel moves to the beginning of the next row After the Do Loop tell Excel to ActiveCell.Offset(1,-4).Select Then set up another Do Loop so that Excel keeps doing this until the whole table has been evaluated

You now have a Do Loop within a Do Loop

Once the first Do Loop is finished it moves to the next row and keeps going until the next row is empty

Advice Writing programs requires trial and error Use the macro recorder to get most of the code Then edit this code to make it do exactly what you want Download a copy of all these notes ( and refer back to them when you have a particular task to performwww.qubexcel.co.uk The only way to get really confident with Excel and VBA is to use them regularly