Top Banner
Sample Copy. Not For Distribution.
21

Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

Jul 20, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

Sample Copy. Not For Distribution.

Page 2: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

i

VBA For Excel

Sample Copy. Not For Distribution.

Page 3: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

ii

Publishing-in-support-of,

EDUCREATION PUBLISHING

RZ 94, Sector - 6, Dwarka, New Delhi - 110075

Shubham Vihar, Mangla, Bilaspur, Chhattisgarh - 495001

Website: www.educreation.in

________________________________________________________________

© Copyright, Author

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form by any means, electronic, mechanical, magnetic, optical, chemical, manual, photocopying, recording or otherwise, without the prior written consent of its writer.

ISBN: 978-1-61813-480-6

Price: ` 285.00

1st Edition: 2016

Revised Edition: 2017

The opinions/ contents expressed in this book are solely of the author and do not represent the opinions/ standings/ thoughts of Educreation. The book is released by using the services of self-publishing house.

Printed in India

Sample Copy. Not For Distribution.

Page 4: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

iii

VBA For Excel Absolutely for Beginner

By

Hirdesh Bhardwaj

EDUCREATION PUBLISHING (Since 2011)

www.educreation.in

Sample Copy. Not For Distribution.

Page 5: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

iv

Sample Copy. Not For Distribution.

Page 6: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

v

About the Author

Hirdesh Bhardwaj, the Founder and CEO

of Webs Jyoti - Development & Training

Organization is known for his best training

and development skills.

Well known as innovator in the field of

IT, he redefined the teaching techniques

with a new & traditional approach. In his

career of 10 years, he has delivered 350+

websites and taught over 5000+ students.

Hirdesh is the author of the “PHP Mysql for Advanced

Learning” series and "Web Designing" books preferred by

most developers and trainees. He is also a certified Oracle

developer and has successfully trained over 5000 students so

far.

He joined NIIT Gurgaon centre as an IT trainer where he

explored the world of Java, PHP, C++, .Net and many more

technologies.

Our Services @ Webs Jyoti

Webs Jyoti is a professional Web development and

Information Technology (IT) organization based in Gurgaon,

offering web designing and Information Technology services

for the global entrepreneurs.

We provide web site development services to clients

across INDIA and other parts of the world. We specialize in

web site design, Education Software‟s, e-commerce & CMS.

Sample Copy. Not For Distribution.

Page 7: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

vi

Sample Copy. Not For Distribution.

Page 8: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

vii

Thank You

I would like to thank all of these people who has always being

supportive from time to time:

My Parents, my God and Goddess, for their kind wishes and

blessings.

My beloved wife Chanchal, appreciate the support provided by

her.

My 2-year old son Vansh, who thinks that his Dad writes such

long books, works for the websites, takes training and

seminars, but wishes it included chapters like Cartoon Corner

and Ninja's Stories.

My Sisters Mona and Preeti (Like a daughter too), who wished

his brother, would devote more time with them instead of

working on this book.

My Younger Brother Jaiprakash, who always complained that

his brother always scold him to work hard and to walk in the

early morning to lose his weight.

My small team @ Webs Jyoti, like a family they think that sir

treat them still as student and force to learn something new

every day.

My creative team, P.SerignNdiaye, Jeff Sidabutar andHernan

Apraez Pastrana and Sarita Sharma who always ask me to put

more and more real time examples from our development

house.

Sample Copy. Not For Distribution.

Page 9: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

viii

Sample Copy. Not For Distribution.

Page 10: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

ix

Sample Copy. Not For Distribution.

Page 11: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

x

Special Thanks

Mr. Rajendra Kapoor

Ms. Manju Sharma

Ms. Sunita Rathi

Ms. Manasi Mohapatra

Ms. Farzana Anjum

Sample Copy. Not For Distribution.

Page 12: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

xi

Sample Copy. Not For Distribution.

Page 13: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

xii

Content list

S.no. Content Page

1. Introduction to VBA Macro 2 - 25

Introduction to VBA Macro Basics and its

programming scope.

2. Dialog Boxes in VBA 26-47

Learn MsgBox, InputBox and various

input/output Windows in VBA.

3. VBA Excel - Cells, Ranges and Offset 48-62

Working with Excel„s Range, Cells and

Offset in VBA.

4. Error Handling in VBA 63-71

Learn how to handle errors and exceptions in

VBA Programming

5. Select Case – VBA 72-77

Learn how to use Select Case (Switch Case)

Statements in VBA.

6. VBA - Loops 78-100

Learn the concepts of loop and flow control

statements in VBA.

7. VBA – Arrays 101-108

Exploring the world of arrays – single and

multi dimensional arrays.

8. VBA - Workbook & Worksheet

operations

109-114

Explore the major activities of Workbook

and worksheet inVBA.

9. VBA - Events 115-121

Learn the different type of Events and

procedure in VBA

Sample Copy. Not For Distribution.

Page 14: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

xiii

10. VBA - Filters and Reporting 122-135

Explore excel Filters (Auto Filter, Advanced

Filters) and Pivot Table using VBA.

11. VBA - User Form & Controls 136-153

Explore the World of GUI- UserForm and

Controls in VBA

12. VBA - Send Automated Mail 154-164

Explore the Automated Mail and outlook

mailing activities in VBA.

13. VBA - Database Connection 165-178

Collecting data from other sources i.e.

Databases, querying data into excel using

VBA DAO etc.

Sample Copy. Not For Distribution.

Page 15: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

Sample Copy. Not For Distribution.

Page 16: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

VBA For Excel

1

Introduction

Completing this book is one of the hardest things that I have

ever done. It took me almost two years to get it into the form

you are reading.

Most of the examples of this book are taken from our training

sessions or from our in-house development series.

More than anything else, I have tried to design this book for a

programmer struggling with a new and complex

programming. I have poured my best efforts into these pages.

I trust you would find this book useful.

We Want to Hear from You!

We value your feedback and want to know what we‟re doing

right, what we could do better, and any other wisdom you‟re

willing to pass our way

When you write, please be sure to include this book‟s title as

well as your name, email address and phone number.

For feedback email: [email protected]

Sample Copy. Not For Distribution.

Page 17: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

Hirdesh Bhardwaj

2

What is a macro?

A macro is a set of code that runs in Office Application and

help us to automate tasks that we frequently use in corporate

world.

Let's assume I should prepare a report in Excel and as for this

I need to use content of many worksheets. In making a report,

I generally use the following steps which are:

-Open Workbook

-Open desired worksheet

-Make selection of the data

-Use Auto Filter and other relevant task

-Copy the desired data after sorting and filtering

-Now Open another workbook / worksheet to paste selected

data for making dashboard

I use all the steps above for so many times to prepare reports

on daily basis data. As you noticed, the steps above is more

likely consuming amount of time and therefore, I use macro

to automate the same task in a single click.

While you are writing the code, you should have a clear idea

of what it does and how it works otherwise your macro may

stuck and your entire work may interrupt. Hence, VBA Macro

is the best solution for the steps above.

So in short, a macro is a recording or capturing of your task

step by step you takes in Excel that you can follow by using a

single button..

Sample Copy. Not For Distribution.

Page 18: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

VBA For Excel

3

What is VBA?

You recently aware with macros. So now let‟s get to know

more with another term called "VBA- Visual Basic for

Applications".

VBA is a programming language, developed by Microsoft.

Before you think about the programming language, it is

essential to know that programming languages are used to

communicate with your computer and not to human.

Therefore, VBA is a tool to communicate and passes your

instructions to Excel.

However, macro is not a programming language. As

explained by John Walkenbach (one of the most popular

author)

"A macro is a sequence of instructions that automates some

aspect of Excel."

Tools

Before you are going to write your first VBA code, you need

to check whether the VBA Editor is installed on your

computer. By pressing ALT+F11 is a quick way to test on

either your Visual Basic has been installed or not. If the VBA

editing window opens you are ok, otherwise you need to

install VBA Editor first.

Sample Copy. Not For Distribution.

Page 19: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

Hirdesh Bhardwaj

4

Macro Security Level

Security is a major key when working with Excel macros. If

you open workbooks containing macros from outside sources,

these files may be harmful to your computer. Excel tool

protects you from running those macros. For the better

performance and avoid security risks you should save your

macro file with .xlsm extension.

There are some macros security level as follow-

Disable all macros without notification: Its allow macros

that are stored in a trusted location. You may manage your

trusted locations from the excel preferences.

Disable all macros with notification: This is default Macro

configuration which displays an alert or warning when a

workbook containing macro code is being open.

Disable all macros except digitally signed macros: By

selecting this option you are allowed to run only those macro

files, digitally signed.

Enable all macros: Allows macros to run without any

interruption or notification. If you are writing your own

macro / VBA code then this could be the recommended

setting for enabling us to run macro files.

Sample Copy. Not For Distribution.

Page 20: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

Get Complete Book At Educreation Store

www.educreation.in

Sample Copy. Not For Distribution.

Page 21: Sample Copy. Not For Distribution. · instructions to Excel. However, macro is not a programming language. As explained by John Walkenbach (one of the most popular author) "A macro

Sample Copy. Not For Distribution.