Excel VBA Programming: Macros (Dr. Tom Co, 9/17/2008)



Similar documents
Excel Programming Tutorial 1

Excel & Visual Basic for Applications (VBA)

Chapter 9 Creating Reports in Excel

VBA PROGRAMMING FOR EXCEL FREDRIC B. GLUCK

A Guide to Getting Started with the AmeriCorps VISTA Applicant Tracking Tool

RIT Installation Instructions

Importing from Tab-Delimited Files

Using Excel to find Perimeter, Area & Volume

Word Processing Features. Creating Tables. Steps to Create a Table. Word Processing Features PowerPoint Handout 3.A.1

Financial Data Access with SQL, Excel & VBA

Formatting Formatting Tables

MY WORLD GIS. Installation Instructions

USING EXCEL 2010 TO SOLVE LINEAR PROGRAMMING PROBLEMS MTH 125 Chapter 4

Guidelines for filling in the Excel Template for Monitoring INSPIRE by the contributing authorities

Word 2010: Mail Merge to with Attachments

LabVIEW Report Generation Toolkit for Microsoft Office User Guide

Programming in Access VBA

Excel 2013 Power Programming with VBA. Mr. Spreadsheet's Bookshelf

Gantt Chart/Diagram Excel Template User Guide

Guidelines for Completing the VDOT Form C 13CPM

Tips on exporting your Oracle Apps reports to Excel. Introduction

LabVIEW Report Generation Toolkit for Microsoft Office

Use signatures in Outlook 2010

Excel Reports and Macros

NEXT-ANALYTICS lets you specify more than one profile View in a single query.

3. (1.0 point) To ungroup worksheets, you can click a sheet of a sheet not in the group. a. index b. panel c. tab d. pane

Automate tasks with Visual Basic macros

TimeSite & ExpenSite Offline Utility 4.0

Microsoft Office Access 2007 which I refer to as Access throughout this book

Managing Contacts in Outlook

Module 2 - Multiplication Table - Part 1-1

Call Recorder Quick CD Access System

Getting Started with Automizy

Service Desk R11.2 Upgrade Procedure - How to export data from USD into MS Excel

Getting Started. Tutorial RIT API

2. The Open dialog box appears and you select Text Files (*.prn,*.txt,*.csv) from the drop-down list in the lower right-hand corner.

Sales Prioritization Views

CREATING FORMAL REPORT. using MICROSOFT WORD. and EXCEL

FOR PREPARING THE CALL REPORT EXCEL DATA FILE AND ELECTRONIC SUBMISSION OF THE CRS AND E-DATA

User Guide for Payroll Service (APS+)

Moving Data Between Access and Excel

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Tommy B. Harrington 104 Azalea Drive Greenville, NC

BUDGET CALL Quick Reference Guide

This activity will guide you to create formulas and use some of the built-in math functions in EXCEL.

How to use MS Excel to regenerate a report from the Report Editor

Create a new investment form and publish it to a SharePoint 2013 forms library

USC Marshall School of Business Marshall Information Services

How Do I Upload Multiple Trucks?

Call Centre Helper - Forecasting Excel Template

Microsoft Excel Introduction to Microsoft Excel 2007

A Comparison of SAS versus Microsoft Excel and Access s Inbuilt VBA Functionality Jozef Tarrant, Amadeus Software Ltd., Oxford, UK

Microsoft SharePoint Products & Technologies

Part A: Introduction to Excel VBA

International Monetary Fund. The Integrated Correspondence System Instructions for Editing Excel-Based Report Forms

Creating an Contact Group (Distribution List) from WebAdvisor

Macros in Word & Excel

Creating a Distribution List from an Excel Spreadsheet

Creating an Excel Database for a Mail Merge on a PC. Excel Spreadsheet Mail Merge. 0 of 8 Mail merge (PC)

Excel basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

Adding A Student Course Survey Link For Fully Online Courses Into A Canvas Course

Simply Accounting Intelligence Tips and Tricks Booklet Vol. 1

Microsoft Access Glossary of Terms

MAS 500 Intelligence Tips and Tricks Booklet Vol. 1

Importing of Clients, Contacts & Leads

EDIT YOUR FUNDRAISING PAGE AND MANAGE YOUR HEADQUARTERS

Creating Codes with Spreadsheet Upload

Siemens Applied Automation Page 1 11/26/03 9:57 PM. Maxum ODBC 3.11

LBi HR HelpDesk: IMPORTING EMPLOYEE DATA

- SAP BusinessObjects and Xcelsius articles, links and ressources

Personal Portfolios on Blackboard

Getting Started with Access 2007

Creating Web Pages with Microsoft FrontPage

Microsoft Access 2010 Part 1: Introduction to Access

SPV Reporting Tool VBA Code User Guide. Last Updated: December, 2009

Installation procedure And Technical specifications

Working with Spreadsheets

Excel Templates. & Quote/Invoice Maker for ACT! Another efficient and affordable ACT! Add-On by V

Excel Add-ins Quick Start Guide

GRS Advantage Website User Reference Guide

FMW-FIR. Financial Information Return For Ontario Municipalities

Mail Merge Creating Mailing Labels 3/23/2011

Document Management in the List View

Virtual Communities Operations Manual

How To Merge Multiple Reports In Jonas With Excel

Mac Outlook Calendar/Scheduler and Tasks

Instructions for Importing (migrating) Data

ACCESS Importing and Exporting Data Files. Information Technology. MS Access 2007 Users Guide. IT Training & Development (818)

Computer Skills: Levels of Proficiency

1. Starting the management of a subscribers list with emill

Why the need for set of rules in Microsoft Outlook?

Creating a Gradebook in Excel

SharePoint List Filter Favorites Installation Instruction

Transcription:

Excel VBA Programming: Macros (Dr. Tom Co, 9/17/2008) Introductory items: 1. A macro (or subroutine) is a group of statements (or code) that performs a set of tasks without yielding a value back. 2. One could usually use the Record option. Often one may need to enhance the macros directly by programming statements. Guidelines for when to create a macro: 1. When the tasks are used often enough in which a template may be not be sufficient. 2. When the benefits (e.g. minimizing human errors in repeated tasks) outweigh the programming effort. Standard Format of Function Sub function_name ( argument_1,argument_2, statements and calculations Example 1. Recording a macro to access text data Preliminary step: download and extract the file: antoine_data1.txt from the URL: http://www.chem.mtu.edu/~tbco/cm3450/antoine_data1.zip Step 1. Open a new Excel spreadsheet and start macro recording by selecting [View] [Macro] [Record Macro ] and select [CTRL-a] as the hotkey. Figure 1. Macro Recording.

Figure 2. Macro Edit window. Step 2. Select from menu: [Data] [Get External Data] [From Text], and choose the file antoine_data1.txt from the location chosen in the preliminary step. Step 3. After you have selected the cell to place the data, stop the recording by clicking on the box in the lower left corner as shown in Figure 3. Figure 3. Stop-Macro-recording button. Step 4. Press [Alt-F11] and you should see that a macro called Macro1 has been programmed. Step 5. Delete the columns containing the data and then test the macro by using the hotkey [CTRL-a]. Question: When you run the macro, does it still asks you to input the file location? Why?

Example 2. Enhancing the data loading macro with additional statements. Step 1. Modify Macro1 by changing the name to get_tp_data. Then add the lines shown in Figure 5 while modifying the destination to $A$1 and name to RawData. Sub get_tp_data() ' Filename = Application.GetOpenFilename( _ FileFilter:= _ "Text Files (*.txt), *.txt, All Files (*.*), *.*", _ Title:="T and P Data File") FileConnection = "TEXT;" & Filename With ActiveSheet.QueryTables.Add( _ Connection:=FileConnection, _ Destination:=Range("$A$1")).Name = "RawData".FieldNames = True....Refresh BackgroundQuery:=False End With Figure 5. Modified macro. Step 2. In the spreadsheet, delete all columns once more and test the new macro. Remarks: 1. VBA is object-oriented programming where each object can have properties or procedures (methods) connected with each object. These are separated by periods (. ). 2. Variables can also be set to cell-ranges. Note the entry in Figure 5 of Range( $A$1 ).

Example 3. Automating the calculation of Antoine Coefficients. Step 1. While inside the module where the other macro resides include the following macro: Sub get_antoine_coefs() Cells(2, 1).Select Set T = Selection Cells(2, 2).Select Set P = Selection T.Name = "T" P.Name = "P" n = P.Cells.Count Cells(1, 3) = "T log(p)" Range(Cells(2, 3), Cells(1 + n, 3)).FormulaArray = "=T*log(P)" Cells(1, 4) = "log(p)" Range(Cells(2, 4), Cells(1 + n, 4)).FormulaArray = "=log(p)" Cells(1, 5) = "T" Range(Cells(2, 5), Cells(1 + n, 5)).FormulaArray = "=T" Set y = Range(Cells(2, 3), Cells(1 + n, 3)) y.name = "y" Set x = Range(Cells(2, 4), Cells(1 + n, 5)) x.name = "x" Cells(1, 6) = "a2" Cells(1, 7) = "a1" Cells(1, 8) = "a0" Range(Cells(2, 6), Cells(2, 8)).FormulaArray = _ "=linest(y,x,true,false)" Cells(3, 10) = "A" Cells(4, 10) = "B" Cells(5, 10) = "C" Cells(3, 11) = Cells(2, 6).Value Cells(4, 11) = -Cells(2, 6).Value * _ Cells(2, 7).Value - Cells(2, 8).Value Cells(5, 11) = -Cells(2, 7).Value Step 2. In the spreadsheet, select [View] [Macro] [View Macros] menu item. Select get_antoine_coefs macro then click [Options ] button and assign the macro with a hotkey, e.g. [CTRL-b]. Step 3. Run the macro.

Remarks: 1. The first three lines selects the range of cells and assigns it to variable T. Cells(2, 1).Select Set T = Selection a) Cells( 2,1 ) is the same as cell $A$2. b) The second line with xldown is the same as the shortcut [CTRL-SHIFT-Down] to select a contiguous range of cells. 2. The line with T.Name = T just gives the cell range an array name. 3. The keyword.formulaarray is needed to set array computation formulas (recall that manually we had to use [CTRL-SHIFT-ENTER]). 4. In the last few lines, we used the format such as : Cells(2,8).Value, to access the number contained in that cell, i.e. in $H$2.