This content has been downloaded from IOPscience. Please scroll down to see the full text. Download details: IP Address: 148.251.232.83 This content was downloaded on 22/02/2019 at 07:30 Please note that terms and conditions apply.
Excel VBA for Physicists A Primer
Excel VBA for Physicists A Primer Bernard V Liengme St Francis Xavier University, Nova Scotia, Canada Morgan & Claypool Publishers
Copyright ª 2016 Morgan & Claypool Publishers All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior permission of the publisher, or as expressly permitted by law or under terms agreed with the appropriate rights organization. Multiple copying is permitted in accordance with the terms of licences issued by the Copyright Licensing Agency, the Copyright Clearance Centre and other reproduction rights organisations. Rights & Permissions To obtain permission to re-use copyrighted material from Morgan & Claypool Publishers, please contact info@morganclaypool.com. Online supplementary data files are available at http://ej.iop.org/images/books/978-1-6817-4461-2/ live/bk978-1-6817-4461-2suppdata.zip ISBN ISBN ISBN 978-1-6817-4461-2 (ebook) 978-1-6817-4460-5 (print) 978-1-6817-4463-6 (mobi) DOI 10.1088/978-1-6817-4461-2 Version: 20161101 IOP Concise Physics ISSN 2053-2571 (online) ISSN 2054-7307 (print) A Morgan & Claypool publication as part of IOP Concise Physics Published by Morgan & Claypool Publishers, 40 Oak Drive, San Rafael, CA, 94903 USA IOP Publishing, Temple Circus, Temple Way, Bristol BS1 6HG, UK
To Pauline, my wife, my friend, and my amanuensis, with many, many thanks.
Contents Preface Acknowledgements Author biography x xi xii 1 Introduction 1-1 1.1 Preparation 1-1 1.2 Demonstrating a simple function 1-3 1.3 Saving a macro-enabled workbook 1-5 1.4 Using constants and VB functions 1-6 1.5 User-defined array function 1-7 1.6 Notes on VBA functions 1-8 1.7 A simple subroutine 1-9 1.8 Linking an image to a subroutine 1-11 1.9 Recording a macro 1-11 1.10 Finding a home for macros 1-14 1.11 Typographical matters 1-14 2 Variables, Dim statements, and data types 2-1 2.1 Naming variables 2-1 2.2 The Dim statement 2-1 2.3 The major reason for variable declarations 2-1 2.4 Declarations in function headers and for constants 2-3 2.5 Data types 2-3 2.6 A second reason for variable declarations 2-4 2.7 Dimensioning arrays 2-4 2.8 The Set statement 2-6 2.9 The With End With structure 2-7 3 Structured programming 3-1 3.1 Branching structures (If and Select Case) 3-1 3.2 Looping structures (For Next and Do While/Until) 3-4 3.3 Some further examples 3-7 vii
Excel VBA for Physicists 4 The Excel object model 4-1 4.1 Examples of properties, methods and events 4-1 4.2 The Range object properties 4-3 4.3 Range object methods 4-5 4.3.1 Select, Copy, and Paste 4-5 4.3.2 Clear methods 4-7 4.3.3 Delete method 4-7 4.4 WorksheetFunction object 4-7 4.5 Workbook and worksheet events 4-12 4.5.1 Workbook event 4-12 4.5.2 Worksheet event 4-13 4.6 Code for sending email 4-14 5 Working with add-ins 5-1 5.1 Creating an add-in 5-1 5.2 Installation 5-2 5.3 Using the add-in 5-2 5.4 Making changes to the add-in 5-3 5.5 Viewing worksheets 5-3 5.6 Protecting the add-in 5-4 5.7 Reversing everything 5-4 6 Numerical integration 6-1 6.1 The trapezoid approximation 6-1 6.2 The Simpson 1/3 approximation 6-4 6.3 An aside 6-6 6.4 Monte Carlo integration 6-7 6.5 Gaussian and Romberg integration 6-8 7 Numerical methods for differential equations 7-1 7.1 Euler s method 7-1 7.2 The Runge Kutta fourth-order method 7-4 7.3 Simultaneous OEDs 7-6 7.4 Example of a system of two OEDs 7-6 7.5 Higher order OEDs 7-8 7.6 R L circuit 7-8 viii
Excel VBA for Physicists 8 Finding roots 8-1 8.1 The bisection method 8-1 8.2 The successive iteration method 8-3 8.3 Root finding with Solver 8-4 8.4 Using range names 8-7 ix
Preface This work will introduce the reader to Visual Basic for Applications (VBA) and demonstrate how it can greatly enhance Microsoft Excel by giving users the ability to make their own functions that can be used within a worksheet, and to create subroutines to perform repetitive actions. Visual Basic (VB, the parent of VBA) has its roots in the educational programming language BASIC redeveloped at Dartmouth College in 1964. In the half-century since then, the language has evolved to become a high-powered object-orientated language which is no longer denigrated by aficionados of language such as C++ and C#. The VBA compiler and editor is incorporated within the Excel application; there is nothing to buy or install. The author s primary aim is to show the reader how to write both functions and subroutines, and to demonstrate that this is not a complex topic. The material is presented in a manner that will allow the reader to experiment with VBA programming; this is the best way to learn it. The scenarios used in the examples center around either fairly simple physics or non-complicated mathematics, such as root finding and numerical integration. The text, therefore, will be suitable not just for physicists but for other scientists and engineers, including students. A tested Excel workbook can be downloaded from here for each chapter. The brevity of the book prevents total rigour so it was necessary to condense some material, but Internet links are provided so the reader can explore VBA topics in greater depth. For the reader who prefers a paper reference source, Excel 2007 VBA by John Green et al (2007, New York: Wiley) is an excellent book, notwithstanding its date; furthermore, it can be obtained very cheaply online. The author is more than happy to reply to email inquiries about topics in the book or other Excel (with or without VBA) questions. Please use the word Excel in the subject line of each message. Have fun learning to extend Excel with VBA! Bernard V Liengme bliengme@stfx.ca x
Acknowledgements The author thanks Microsoft Excel Most Valuable Professionals (MVPs) Jon Peltier, Bob Umlas, and Jan Karel Pieterse for their help over the years. xi
Author biography Bernard V Liengme Bernard V Liengme attended Imperial College London for his undergraduate and postgraduate degrees; he held post-doctoral fellowships at Carnegie-Mellon University and the University of British Columbia. He has conducted extensive research in surface chemistry and the Mossbauer effect. He has been at St Francis Xavier University in Canada since 1968 as a Professor, Associate Dean, and Registrar, as well as teaching chemistry and computer science. He currently lectures part-time on business information systems. Bernard is also the author of other successful books: COBOL by Command (1996), A Guide to Microsoft Excel for Scientists and Engineers (now in its fourth edition), A Guide to Microsoft Excel for Business and Management (now in its second edition), Modelling Physics with Microsoft Excel (2014), and SMath for Physics: A Primer (2015). xii