100% found this document useful (5 votes)
24 views84 pages

Excel VBA Step by Step Guide To Learning Excel Programming Language For Beginners 1st Edition by Jason Jay ISBN 1976198127 9781976198120 Download

The document is a guide for beginners on learning Excel VBA, detailing its functionalities and how to create macros for automating tasks in Excel. It includes step-by-step instructions on accessing the Developer Tab, creating and running macros, and saving macro-enabled workbooks. The guide also covers fundamental programming concepts and provides quizzes to reinforce learning.

Uploaded by

agheadqahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
24 views84 pages

Excel VBA Step by Step Guide To Learning Excel Programming Language For Beginners 1st Edition by Jason Jay ISBN 1976198127 9781976198120 Download

The document is a guide for beginners on learning Excel VBA, detailing its functionalities and how to create macros for automating tasks in Excel. It includes step-by-step instructions on accessing the Developer Tab, creating and running macros, and saving macro-enabled workbooks. The guide also covers fundamental programming concepts and provides quizzes to reinforce learning.

Uploaded by

agheadqahar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

Excel VBA Step By Step Guide to Learning Excel

Programming Language for Beginners 1st Edition


by Jason Jay ISBN 1976198127 9781976198120
download
https://ebookball.com/product/excel-vba-step-by-step-guide-to-
learning-excel-programming-language-for-beginners-1st-edition-by-
jason-jay-isbn-1976198127-9781976198120-15878/

Instantly Access and Download Textbook at https://ebookball.com


Get Your Digital Files Instantly: PDF, ePub, MOBI and More
Quick Digital Downloads: PDF, ePub, MOBI and Other Formats

Java Easy Java Programming for Beginners Step By Step Guide to


Learning Java 1st Edition by Felix Alvaro ISBN 1539509362
9781539509363

https://ebookball.com/product/java-easy-java-programming-for-
beginners-step-by-step-guide-to-learning-java-1st-edition-by-
felix-alvaro-isbn-1539509362-9781539509363-15874/

Java programming A beginners guide to java Step by Step 1st Edition


by Troy Dimes 1507562942 978-1507562949

https://ebookball.com/product/java-programming-a-beginners-guide-
to-java-step-by-step-1st-edition-by-troy-
dimes-1507562942-978-1507562949-17382/

Microsoft Excel 2016 Step by Step 1st Edition by Curtis Frye ISBN
0735697469 9780735697461

https://ebookball.com/product/microsoft-excel-2016-step-by-
step-1st-edition-by-curtis-frye-
isbn-0735697469-9780735697461-16138/

PYTHON'S COMPANION A STEP BY STEP GUIDE FOR BEGINNERS TO STAR


TODAY 1st edition by Joe Thompson ISBN 0123456789 978-0123456789

https://ebookball.com/product/python-s-companion-a-step-by-step-
guide-for-beginners-to-start-coding-today-1st-edition-by-joe-
thompson-isbn-0123456789-978-0123456789-16686/
Excel 2016 Power Programming With VBA 1st Edition by Michael
Alexander, Richard Kusleika ISBN 1119067723 9781119067726

https://ebookball.com/product/excel-2016-power-programming-with-
vba-1st-edition-by-michael-alexander-richard-kusleika-
isbn-1119067723-9781119067726-15876/

Research Methodology A Step By Step Guide for Beginners 3rd Edition by


Ranjit Kumar 1849203016 978-1849203012

https://ebookball.com/product/research-methodology-a-step-by-
step-guide-for-beginners-3rd-edition-by-ranjit-
kumar-1849203016-978-1849203012-17356/

Machine Learning Step by Step Guide To Implement Machine Learning


Algorithms with Python 1st Edition by Rudolph Russell ISBN
9781719528405

https://ebookball.com/product/machine-learning-step-by-step-
guide-to-implement-machine-learning-algorithms-with-python-1st-
edition-by-rudolph-russell-isbn-9781719528405-16086/

Assembly Language Step by Step Programming with DOS and Linux 2nd
Edition by Jeff Duntemann ISBN 0471375233 9780471375234

https://ebookball.com/product/assembly-language-step-by-step-
programming-with-dos-and-linux-2nd-edition-by-jeff-duntemann-
isbn-0471375233-9780471375234-12520/

(Ebook PDF) RESEARCH METHODOLOGY a step by step guide for beginners


1st edition by Ranjit Kumar 1849203016‎ 978-1849203012 full
chapters

https://ebookball.com/product/ebook-pdf-research-methodology-a-
step-by-step-guide-for-beginners-1st-edition-by-ranjit-
kumar-1849203016aeurz-978-1849203012-full-chapters-23028/
EXCEL VBA

Step-By-Step Guide To Learning Excel


Programming Language For Beginners

Jason Jay
© Copyright 2017 by Jason Jay - All rights reserved.

If you would like to share this book with another person, please purchase an
additional copy for each recipient. Thank you for respecting the hard work of
this author. Otherwise, the transmission, duplication or reproduction of any of

the following work including specific information will be considered an


illegal act irrespective of if it is done electronically or in print. This extends
to creating a secondary or tertiary copy of the work or a recorded copy and is
only allowed with express written consent from the Publisher. All additional
right reserved.
TABLE OF CONTENT
EXCEL VBA 1
Introduction 5
CHAPTER 1 7
VBA Developer TAB 7
Accesing to the Developer TAB 7
Quiz 1 8
CHAPTER 2 9
Macros 9
Creating a Macro 9
Relative References 11
Running the Macro 12
Saving a Macro-Enabled Workbook 12
Quiz 2 14
CHAPTER 3 16
Starting with VBA 16
What if I need to fill the cells up to 100? 19
Insert Form Button 21
Simple things a Macro can’t do. 22
Insert ActiveX Button 22
Variables, Do and Loop. 23
What is the advantage of declaring variables as byte, integer or any other? 31
APPs Performance 35
MSGBOX 39
If and Select Case 40
Quiz 3 43
CHAPTER 4 45
Project: Creating a Simple Calculator using ActiveX 45
What is a Module? 45
How to create a Module 46
Adding Letters? 58
Quiz 4 59
CHAPTER 5 60
Project: Calculator using Forms 60
Review 60
FORMS 61
Commmand Buttons code 65
Excel Formulas on VBA 74
Combining VBA and a Spreadsheet 75
Starting with Declarations: 78
Open and Close declarations: Displaying a form without looking any spreadsheet 81
Macro Security 87
Comments 89
The whole code for Calculator Project 94
Command Buttons Order 101
Adding Password to VBA Code. 102
Quiz 5 104
Interacting with other Applications. 105
Opening other apps from Excel 105
Sending an Outlook e-mail from Excel: 108
Exercises Solutions: 111
Answers Chapter 1 111
Answers Chapter 2 112
Answers Chapter 3 113
Answers Chapter 4 114
Answers Chapter 5 115
Introduction

If you already know how to use Microsoft Excel but there’re a few
things you can’t do, it is time to learn the strongest functionality it has, Visual
Basic for Applications (VBA).
Visual Basic for Applications is a programming language incorporated
in Microsoft Excel, Access, PowerPoint and even Word, which let you do all
things you already know about them and much more. For example, you want
that every time you open a specific Microsoft Word file it writes
automatically the current date two lines below where you left last time. Or
maybe you want a whole spreadsheet of Excel without formulas on it and still
applying them as if they were there. How would you do that? All these things
and much more are done with Visual Basic for Applications for Microsoft
Office.
Look at the example below:

It looks like a program made for analysis, and it does. Guess what
program it is? Probably you are thinking it is not any Microsoft Office
program, but let me tell you that it was made with Microsoft Excel, how
would you do something like that without programming? There’s no way!
You’ll learn much more than that and will be able to create your own
programs using Visual Basic for Applications (VBA).
If you need a very specific program for your business analysis,
something for personal use, or even just for having fun, you need Visual
Basic for Applications now!
CHAPTER 1
VBA Developer TAB

Every programming language has similarities between their


fundamentals. The functions IF, Then, Loop, Close, Open, are just some of
them. We’ll learn the basic ones first; it will be necessary to understand how
VBA works.

We’ll use Microsoft Excel 365 for this instructions and examples, however,
from Microsoft Excel 2007 onwards it will work the same.
Accesing to the Developer TAB

Microsoft Excel doesn’t show the Developer TAB by default. It only has
File, Home, Insert, etc. But there’s no one called Developer. To access to this
TAB there are different options, but we’ll show the easiest one.
1. Right click to the Ribbon (any part inside the red box, except the
buttons).

2. Select Customize the Ribbon

3. Enable the Developer checkbox and click the OK button.


4. You should see the Developer TAB available now.

Quiz 1

1. How you Access the Developer TAB?


a) It is available by default in Excel.
b) Right click on the Ribbon, Customize the Ribbon, enable the
Checkbox for Developer and Accept.
c) Go to file, Options, Advanced and Enable the Developer TAB.
CHAPTER 2
Macros

Creating a Macro
You’ll see a few options available in the Developer TAB, by now we’ll start
to use the Record Macro button.
A Macro is an automated sequence which will apply every time you play it.
Let’s see a practical example of it:
Imagine that in your job you do the same process every morning. It takes
some valuable time and even you’re getting bored of that.
The process is the following:
a) You receive a Microsoft Excel file from your boss with some data
and you need to write the date using Year, Month and Day in different
columns.
You do this because it is the format your job needs and you’ve been adding
the same values every day for a few years.
In this case an semi-automated process would be helpful. Excel gives that
option to all of us with Macros. A Macro is a semi-automated process which
let you run a specific task using a shortcut.
To create a Macro, follow the sequence below:
a) Click the Record a Macro Button.
b) Write a name for your Macro. (Needed)
c) A shortkey which every time you press will Run the Macro. Be
careful, don’t add Ctrl + C or Ctrl + v, otherwise it won’t copy or paste
anymore, but run the Macro. In case you want a more specific shortcut,
hold the shift key as you press a letter. For example, ctrl + shift + c. To
make it work, don’t press ctrl as you add a short cut. (Optional)
d) Store Macro in: Personal workbook: Will be available for all the
files you open with Excel on that computer; New Workbook will be
available for a new file only. This workbook, will apply only to the
current open file. (Needed to choose one)
e) Write a description about what that Macro does. (Optional)
f) Click Ok.

g) Start doing everything you always do, which would be adding the
current date in this case.
h) Once you finish, go back to the Record Macro Button, which now is
called Stop Recording. Press it and now should be saved.
This would be a very simple Macro, it only adds the current date, but what
would you think if you also need to import data from a web page which is
updated every hour, and need to classify it using a few charts, and you do the
same process several times a day. No doubt, a good Macro would be useful.
The process to get it any Macro, is the same we’ve followed. There’s only an
important thing to consider when creating one, it is to choose between using
Relative References or not.

Relative References
The Relative References button is just below the Record a Macro Button.
Once you click on it, it remains active until you click on it again. It is used to
record macros in which the process should be applied to different ranges
instead of one already set.
Its functionality is very useful. A macro recorded without relative references
will always repeat the process on the same cells used when recorded. But if
you use relative references, the macro will run from the active cell. Using the
example above, what If you need the dates written on cells F4:H4 instead of
B2:D2? The only thing you should do is to select F4 and run the Macro. Or
select any cell you need and run it. But you need to record the Macro using
Relative References, and then select the cell and run it, otherwise it wouldn’t
work.

Running the Macro


There are a bunch of ways to run a Macro. Let’s see the first: Run this one by
clicking on the Macros Button, then click on it to run it.
Maybe it is not as practical as we expected, however, we’ll add more
functionality and make it easier to run in the next step.

Saving a Macro-Enabled Workbook


Once you have added some Macros to your worksheet and try to save it
you’ll get a notification like the following:

This might be a little tricky, because most people would attempt saving the
file without reading this notification:
The following features cannot be saved in macro-free workbooks:
°VBA project
To save a file with these features, click No, and then choose a macro-enabled
file in the File Type list.
To continue saving as a macro-free workbook, click Yes.
Most people would just click Yes, and according to this message they
wouldn’t save the file with their Macros, but as Macro free. It would make
you lose all your Macro work.
To save the Macro, just click No to the message above, then select save as
Excel Macro-Enabled Workbook.
Click on Save, and it is done!
Once you open it again, you should see a message saying Macros have been
disabled, and a Button saying Enable Macros. Click on it and won’t have
further problems. If you don’t click on it, you won’t be able to work with
VBA, at least you enable them on Macro Security in the Developer TAB, or
follow the steps on Chapter 5: Macro Security.

Quiz 2
1. What is a Macro?
a) It is an Excel Formula
b) It is a shortcut which runs a recorded process.
c) It is a built-in process included in Excel.

2. How to create a Macro?


a) Clicking on the Visual Basic Button
b) Clicking on the Macros Button
c) Clicking on the Record Macro Button

3. What is Relative References for?


a) It is to Record a Macro without set specific cells.
b) It is to Record a Macro with specific cells.
c) Without it a Macro isn’t editable.

4. How to Run a Macro?


a) Clicking on the Macro Button
b) Clicking on the Record Macro Button
c) Clicking on the Relative References Button

5. How to save a workbook with Macros?


a) Just save the file normally, the Macros will be saved.
b) You’ll get a notification, in which we should be denied and then
select save as Macro-Enabled Workbook.
c) You’ll get a notification, in which we will be notified that we are
saving a Macro-enabled workbook, then just accept to save.
CHAPTER 3
Starting with VBA

Macros are fundamental to be introduced to VBA.


Let’s see why by following the process:
1.-Create a new Macro without relative references.
2.-In the process select the cell A1, write a number 1, and press enter.
3.-Stop Recording.

Next to the Record Macro button, there’s another one called Visual Basic.
Click on it and you’ll see a code like this:

Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
Range("A2").Select

Congrats, you have some Visual Basic Code now. It means a few orders:
Select Cell A1
Write the number 1
Select Cell A2

It is exactly what we did. But now, we’ll edit the code so that the Macro does
something else.
If we see, there’s a pattern, which is: select, write, select. So, we could
continue the pattern by adding a few more things directly, like this:

Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
Range("A2").Select
ActiveCell.FormulaR1C1 = "2"
Range("A3").Select
ActiveCell.FormulaR1C1 = "3"
Range("A4").Select

Now that you added this, run the macro, but now you’ll see a second way to
run it. Press the green button above the code. Once you click on it go to the
Excel Spreadsheet by clicking on the Excel symbol. You’ll see that the cells
A1:A3 are filled with the numbers we wrote and the Cell A4 is selected.
It may look very complex, and it is like that because humans use to do things
that machines wouldn’t. I needed to select cell A1, A2, A3 and son on. But
does Excel really need to select it to write a simple number?
Let’s try it by adding the following code:

Range("A1") = 1
Range("A2") = 2
Range("A3") = 3

That’s it! Excel can skip steps humans can’t. So, it makes it much more faster
and easy to accomplish its work.

What if I need to fill the cells up to 100?


Now, I think that if I need to follow this pattern until A100 is going to be
hard. What should I do?
Let’s add this:

Range("A1:A100") = 1

Oops! It adds a number 1 to all cells from A1 to A100. Let’s try another
thing:

Range("A1:A100") = 1 + 1

It adds a number two instead. So, How do I tell Excel that I want it to fill
cells in sequence?

There are several ways. One is to record a Macro and during the process add
a number 1 to the cell A1, then hold right click on the small square and scroll
down until you select A100. Select fill series. Go to Visual Basic and you’ll
see a code like this:

Range("A1").Select
ActiveCell.FormulaR1C1 = "1"
Selection.AutoFill Destination:=Range("A1:A100"),
Type:=xlFillSeries
Range("A1:A100").Select
It works! I think we already have a great idea about how to get the VBA code
we want. Record a Macro, and there it is. However, there’re a lot of functions
that Macros can’t offer. For example, fill up cells in sequence according to
the number written in cell B2, and once it changes fill up the sequence again
according to that number. So, if there’s a number 1 it will be filled one by
one, if I change it to five, it’ll go five by five and so on. How are you going
to record that in a Macro? What if it is not just filling up a sequence, but a
Financial matter, working with real numbers and you need to solve a problem
like this fast?
Recording a Macro isn’t enough always, but in most cases, it helps.
Visual Basic Application (VBA) is a programming language, but it is not
necessary to know VBA code or computer programming if the Macro
Recorder does what you want.
You should know that when you record a Macro it records even your
mistakes, and it will repeat them when you run it. If you want to solve a
problem like this you have two options:
1. Record the Macro again.
2. Edit the VBA code.
Remember that recording a good Macro or writing a good VBA code,
will make Excel to run smoothly. Otherwise, you can expect a not
responding message until it finishes or maybe it even could stop
working.
We’ll focus on things you can’t do using only recorded macros. So, you can
learn how powerful Visual Basic for Applications is in Microsoft Excel.

Insert Form Button


We have some Macros recorded now. But we see that to run them it is not
that handy, so what if we add a button, and every time we press it on the
macro runs?
To do that, click on the insert button in the Developer TAB. You’ll see it
displays two boxes, called form controls and ActiveX Controls. Let’s select
the Form Control Button, then wherever you want on the spreadsheet hold
and drag to create the size of the button. Once you stop holding you’ll see a
new window in which you will be able to choose the Macro you want the
Button to run. Select it and it’s done.
If you want to change the name of the button just click on it twice slowly or
right click, edit text. To move the position of the button, just right click, and
then hold and drag with the left click.

Easy, don’t you think?

Simple things a Macro can’t do.


Insert ActiveX Button
Let’s add an ActiveX Button. To do this in the Developer TAB click on
Insert, and then select ActiveX Button. Hold and drag to create the button in
the spreadsheet. This kind of button are very different than Form Buttons,
because these ones work directly with VBA.
Let’s add some functionality which can’t be done using Macros only. We
want that button to create a sequence of numbers according to the value
inserted in cell B2. I mean, if I add number 1 then I’ll see a pattern going one
by one. If I write 5, then it will go five by five, and so on, from A1 to A100.
This is a great opportunity to explain Variables.

Variables, Do and Loop.


A variable is an algebraic term, which is usually represented by a letter and
its value varies, for example, x + y = 10, in this equation there are two
variables, X and Y. They can vary to equal 10. If X = 5 and Y = 5 then
x+y=10 , and it is also correct if X= 8 and Y=2, because X+Y= 8+2 = 10.
Etc.

In VBA we declare variables too. In this case we’ll add some functionality to
the ActiveX Button we added in the previous step.
First, we’ll need to choose the correct numeric variable.

These are the most types of variables, check them out and see their storage
size and range by now:

Data type Storage Range


size
Byte 1 byte 0 to 255

Boolean 2 bytes True or False

Integer 2 bytes -32,768 to 32,767

Long 4 bytes -2,147,483,648 to 2,147,483,647


(long integer)
Single 4 bytes -3.402823E38 to -1.401298E-45 for negative
(single- values; 1.401298E-45 to 3.402823E38 for
precision positive values
floating-point)
Double 8 bytes -1.79769313486231E308 to
(double- -4.94065645841247E-324 for negative values;
precision 4.94065645841247E-324 to
floating-point) 1.79769313486232E308 for positive values

Currency 8 bytes -922,337,203,685,477.5808 to


(scaled integer) 922,337,203,685,477.5807

Decimal 14 bytes +/-79,228,162,514,264,337,593,543,950,335


with no decimal point;
+/-7.9228162514264337593543950335 with 28
places to the right of the decimal; smallest non-
zero number is
+/-0.0000000000000000000000000001

Date 8 bytes January 1, 100 to December 31, 9999

Object 4 bytes Any Object reference


String 10 bytes + 0 to approximately 2 billion
(variable- string
length) length
String Length of 1 to approximately 65,400
(fixed-length) string
Variant 16 bytes Any numeric value up to the range of a Double
(with numbers)
Variant 22 bytes + Same range as for variable-length String
(with string
characters) length
User-defined Number The range of each element is the same as the
(using Type) required by range of its data type.
elements

In this table, we see that each variable has different ranges, some larger than
others, and in the same time the size storage varies too. The smallest unit of
memory available is called a byte, and according to this table it goes from
number 0 to 255.
Let’s do this to start:
1. Enable the Design Mode Button, which is next to the Insert Button in
the Developer TAB.
2. Double click on the ActiveX Button.
3. Now you’re ready to write the code for it.

Private Sub CommandButton1_Click()


End Sub
That’s what you should see.
Now let’s write some code between those lines we see:

Private Sub CommandButton1_Click()


Dim X As Byte
Dim Y As Byte

X=1
Y = Range("B1")
Do
Range("A" & X) = Y
X=X+1
Y = Y + Range("B1")
Loop Until X = 101
End Sub

This is the meaning of this code. It says:


Private Sub CommandButton1_Click()
Private means that you won’t find the code in the Macros Button.
CommandButton1 is the default name given to the ActiveX Button, and Click
means that the code below will be applied only when you click on it.
Dim X As Byte
Dim declares variables, So, it says that X is a variable that will be used
with numbers from 0 to 255, because “Byte” only accepts that range
according to the table above.
X=1
I added the initial value for the variable, which will be 1.
Y=Range(“B1”)
Means that the value of the variable Y is the same value as cell B1.
Do
Means do this.
Range(“A” & X) = Y
We already declared X as a variable with a value of 1. So, the Range
AX means Range(“A1”)=Y, and Y is the Range (“B1”). If I add the number
5 to B1, then Y = 5. So, finally it is Range(“A1”)=5.
X=X +1
It means that X=1+1 because in that moment X=1, so finally X=2. The next
time this process repeats, it will be X=X+1 again, but this time X=2, so
finally X=3, because X=2+1, and so on.

Y = Y + Range("B1")
This will follow the same process as the last step, but with the value added in
Range(“B1”) which will be that one we added. If B1 has a 5, then it will add
another 5, following a sequence according to what we wrote on B1.
Loop Until X = 101
It means that the same process between Do and LOOP will be repeated until
X=101.
End Sub
Means the end of the procedure.

Let’s try it now!


Add the number 1 to the cell B1 and press the button. You’ll see that it fills
up the cells from A1 to A100 one by one. Try it with two, and you’ll see that
it adds from A1:A100 two by two. Finally add the number 3 and press the
button.
Congrats! You’ve found your first bug! It is an overflow.

Bugs are errors when one writes a wrong code. In our example the code
works fine with numbers from 0 to 255 only, and once we ask the file to go 3
by 3 one hundred times we are expecting it to go from 3 to 300, which is
more than 255, so it causes an overflow.
To fix it, we have two options, we increase the value of the variable or we let
Excel to add it automatically.
Let’s consider the possibility of increasing the capacity of the variable, we
can change it from byte (0 to 255) to integer (-32,768 to 32,767). It will be
great if we were pretty sure that we won’t work with higher numbers of
32,767, or even change it to “long” which goes from -2,147,483,648 to
2,147,483,647. It would be great to work even with millions, but let’s
imagine that someone needs to put billions in B1. In that case even a long
variable wouldn’t be enough. So, the best way to solve this is don’t declare
the variable and leaving the code in the following way:
Private Sub CommandButton1_Click()
Dim X As Byte
X=1
Y = Range("B1")
Do
Range("A" & X) = Y
X=X+1
Y = Y + Range("B1")
Loop Until X = 101
End Sub

Or declaring the value as Variant, which let any number or letters.

Private Sub CommandButton1_Click()


Dim X As Byte
Dim Y As Variant
X=1
Y = Range("B1")
Do
Range("A" & X) = Y
X=X+1
Y = Y + Range("B1")
Loop Until X = 101

End Sub

What is the advantage of declaring variables as byte, integer or any


other?
Maybe you are wondering why to declare a variable as byte or integer, etc. If
Excel can add one automatically as in the example we’ve seen?
The answer is simple and important to know. Let’s remember that a variable
as byte stores in RAM Memory only 1 byte, which is the smallest amount of
memory possible in Excel, but a variant declared as variant stores at least 22
bytes. If we don’t declare a value in which we are sure won’t add more than
the needed values, it will consume more RAM, it will cause a program to run
slow in very simple tasks.
For example, I need to declare 1000 variables as byte. It would consume 1
Kilobyte of RAM only.
But By default, if you don't supply a data type, the variable is declared as
Variant data type, which consumes 22 bytes + string length, string length is
for letters.
So, 1000 variables not declared would be automatically declared as variant,
and it would consume 22 kilobytes. It is 21 empty kilobytes, which is 95% of
not available space and don’t used at all in case we would have required them
as bytes instead. If you are going to program in VBA and want your program
to run smoothly, you better learn what kind of variable to declare. Otherwise,
it will probably take long to load and then will run slow.

Require Variable Declaration Importance

Probably, one of the biggest problems about not declaring a Variable are
creating bugs, because we simply type something wrong. Let’s make a simple
example of it.

1. Open Visual Basic


2. Double Click on ThisWorkbook
3. Add the following code:

Public Sub Infinite()


myvariable = 200
myrange = 1
Do Until myvariabe = 300
Range("A" & myrange) = myvariable
myvariable = myvariable + 1
myrange = myrange + 1
Loop
End Sub

As you see it is a very similar code to one we did before. It should stop once
myvariable equals 300, but it continues. Can you see the problem?
If not, run the code. You’ll notice that it will go beyond 300, and practically
will have no end, or at least until it has no more files. In Excel 365 the total
files available is 1048576, so it will go until A1048576 and will enter on an
error.
If you don’t want to wait until that, Press ESC Key which will interrupt the
code execution and then press End Button.

Well, the problem is that we miss just one letter of a variable. We named it
myvariable but when coding we miss the letter L : Do Until myvariabe. Excel
identified myvariable and myvariabe as two different variables, and
automatically assigns them as Variant type.
What would have happened if your code has a lot of code? To find the
problem you should look at all your code to find the letter you missed! As
you see it would be a big trouble! That’s why Microsoft Excel has an option
called Require Variable Declaration.
It won’t let you write anything which is not code or a declared variable. To
enable this feature do the following:

1. Visual Basic
2. Tools Tab
3. Options
4. Require Variable Declaration
5. Ok.

It will add something above the code, which says Option Explicit. But, you
won’t see that change until you start a new project, insert a module or you
write it by yourself. That’s why it is important to start a new project always
with this option enabled.
In this case, add Option Explicit manually above the code.

Option Explicit
Public Sub Infinite()
myvariable = 200
myrange = 1
Do Until myvariabe = 300
Range("A" & myrange) = myvariable
myvariable = myvariable + 1
myrange = myrange + 1
Loop
End Sub

After you added this code Run it!


You’ll see that it automatically displays a message saying Variable Not
Defined and even highlight the problem. So, you can see that it will avoid
further problems for misspell variables.

APPs Performance

Now, imagine that you really need to fill one by one all the cells from A1 to
A1048576, and that it is one of the functionalities your APP should do.
Almost always Developers are concerned about creating Apps which run fast.
In Excel it is not an exception. Try running the following code and see how
long it takes to fill all column A with numbers:

Public Sub FillColumnA()


Dim X As Long
X=1
Do Until Range("A1048576") <> Empty
Range("A" & X) = X
X=X+1
Loop
MsgBox "Finished"
End Sub

When it finishes it will display a message saying “Finished”. It will probably


take about 5 minutes, maybe less or more depending on your computer
performance. You probably will notice Excel saying that it is not responding,
but in most cases it is still working. I don’t want to wait too long!
A good way VBA Developers make Apps run faster, is by disabling the
ScreenUpdating. Every change Excel does we are supposed to see it. But, if
we don’t want to see it, it will definitively improve its performance.
Let’s change the code to this:
Public Sub FillColumnA()
Dim X As Long
On Error GoTo A
Application.ScreenUpdating = False
X=1
Do Until Range("A1048576") <> Empty
Range("A" & X) = X
X=X+1
Loop
A:
MsgBox "Finish"
End Sub

Run it and time how long it takes, then compare how faster this way is. Don’t
forget to erase Column A before you start. You should notice a big
difference! Screenupdating is a great tool when need to increase an App
performance. However, now go to the spreadsheet and try to do something.
You should notice that it is not possible to do anything, or at least you won’t
see that, because screenudating = false turned off the visibility changes. So,
when you use Application.screenupdating = false NEVER forget to add
Application.ScreenUpdating = True at then end of your code, and even better
as a backup in case of error do as the code below:

Public Sub FillColumnA()


Dim X As Long
On Error GoTo A
Application.ScreenUpdating = False
X=1
Do Until Range("A1048576") <> Empty
Range("A" & X) = X
X=X+1
Loop
A:
Application.ScreenUpdating = True
MsgBox "Finished"
End Sub
MSGBOX

As you saw in the code above, it displays a message saying “Finished”. We


can do much more than that!
One of those messages can execute code too! Look at the table below:

Constant Value
vbOK 1
vbCancel 2
vbAbort 3
vbRetry 4
vbIgnore 5
vbYes 6
vbNo 7

Those are the kind of messages you can display, and once you click on it, it
will return the value according to the table above.
Run the following code:
Public Sub fff()
Dim X As Byte
X = MsgBox("This is the body",
vbYesNoCancel, "This is the title")
MsgBox X
End Sub
It will display a msgbox with a Yes, No and Cancel button. Once you click
on any button, X will take that value and will display it on the next MsgBox.
So, if you click on Yes, you’ll see a number 6, and if you click on No, you’ll
see a number 7.
This is a great advantage, because we can ask if user want to proceed or
not, etc.

If and Select Case

We see that once we are adding a msgbox, we can also create a warning
message, or information, or critical, etc. Let’s combine one in the code
below, so we can figure out how to do it:

Public Sub myMessageBox()


Dim X As Byte

X = MsgBox("I'll tell you the button you pressed on", vbYesNoCancel +


vbExclamation, "What Button would you press?")
If X = 2 Then
MsgBox "You Pressed Button Cancel"
ElseIf X = 6 Then
MsgBox "You pressed Button Yes"
Else
MsgBox "You pressed Button No"
End If

End Sub
It will tell you the button you pressed. It means that if you can know
what button is pressed on then you can control an action according to it. For
Example execute a Macro.

If is the conditional. Like:


Public Sub Evaluation()
Dim X As Byte

X = Range("A1")
If X = 5 Then
MsgBox "There's a number " & X
ElseIf X = 6 Then
MsgBox "There's a number " & X
ElseIf X = 7 Then
MsgBox "There's a number " & X
ElseIf X = 8 Then
MsgBox "There's a number " & X
Else
MsgBox "There's another value"
End If
End Sub
So “if” makes a conditional, “elseif” is checked in case the first condition
doesn’t meet, and will check as many “elseif” as you add until one condition
meet. “Else” will run in case any condition doesn’t meet. So, if we don’t add
any number from 5 to 8, we’ll see a message saying “There’s another value”,
but if we put a number 8, then we’ll see a message saying There’s a number
8.
If, elseif and else are very similar to another kind of code called Select Case,
Case, and Case Else However, this one is more efficient than IF in some
cases. Let’s do the same than above but using case instead:

Public Sub Cases()


Dim X As Byte
X = Range("A1")
Select Case Range("A1")
Case 5 To 8
MsgBox "There's a number " & X
Case Else
MsgBox "There's another value"
End Select
End Sub

This last one occupies much less code. It was easier and better. You can do
practically the same but is up to you which one you want to use.
Quiz 3

What is a Variable?
a) It is a value which never changes.
b) It is a value which changes.
c) It is a special number.

What is the storage size of a byte variable and its range?


a) It stores 2 bytes and goes from -32567 to 32567.
b) It stores 1 byte and goes from -256 to -256.
c) It stores 1 byte and goes from 0 to 256.

What is the advantage of declaring variables?


a) It consumes less RAM and makes it run smoothly.
b) It consumes more RAM and makes it run slower.
c) It wouldn’t work if variables aren’t declared.

Is it obligatory to declare variables?


a) Only if Require Variable Declaration is enabled
b) It is obligatory in all circumstances
c) It is not, Excel declares them automatically according to the value
added.

Msgbox returns a value depending on the button pressed:


a) We assign the value to each button
b) It returns a value depending on the button pressed
c) Values aren’t returned
CHAPTER 4
Project: Creating a Simple Calculator using
ActiveX

We’re going to create a simple calculator first. After this project is


completed, we’ll create a more sophisticated one. To do that, we’ll require to
know the following:

What is a Module?
When we start writing VBA code, we’ll usually start writing on Sheet1. But,
in order to understand Modules, Procedures Private and Public, we’ll create a
visual calculator. First, we´ll do it on a spreadsheet, and then we’ll do it as a
real program.

A module, is something like a Box, in which we add some code to run when
we “call” it. To understand how it works, we’ll create our calculator using a
few Modules.

How to create a Module


First, add the following ActiveX buttons on a spreadsheet on the table as it is
shown on the image. To add the appropriate symbol to each button, it is not
as we did on the Form Buttons. In this case we have to click on the Design
More Button, which is in the developer TAB, then right click on each button
and you’ll see something like the image below, in which we see something
called “Name”. Most people would think that there’s where we can add the
correct symbol to see, but that’s wrong. Excel identify the Button with that
name. Imagine you have several buttons with + as their name. How would
either Excel or you identify which one is the correct? So, every button will
have a different name, let’s add a good one which let us know what it does
like In the example I wrote cmdAddition. It lets me know that that it applies

to the “+” Button. However, how can I add the sign +?

In the image above, you ‘ll see that there’s a property called Caption. That’s
where You’ll add the “+” sign.
You can even play a little bit with the other options, like the Backcolor, Font,
Height and even add a picture. I left all options as they were by default.

Once you repeated the process for all the buttons, let’s start with the next
steps. Let’s create a Module!
Follow these steps:

1. Open Visual Basic through the Developer TAB. By default you’ll see
something like this:

2. Click on “View TAB” and select “Properties Window”. We’ll always


use this window a lot. Now you should see something like what’s
shown inside the red square:
3. Let’s add our first Module. Click on Insert TAB, then Module.

4. You’ll see a New Folder Called “Modules” with a file there Called
“Module 1”:

5. Select it and in the Properties Window Change its name to Addition,


then repeat the process until you have created a Module for Minus,
Division and Times.
6. Double click on the Module “Addition” to open it.
7. You’ll see that it displays an entire blank sheet. We need to add a
Procedure! To do that, click on Insert TAB, then Procedure. It will
display a window like this, add a name and leave the default options:

8. Click OK and you’ll see this:

9. Between those lines write the following code:

Range ("A2") + Range("B2") = Range("C2")

10. Now run it by clicking the green triangle above.


11. It is displaying an error saying: Invalid use or Property.
Maybe it is because we haven’t added any number in the cells A2 and
B2. Add them and run it again.

It is still having a problem! Can you see what’s wrong?

Welcome to your second bug! The problem here is a very usual one. We are
asking Excel that cells A2 + B2 are equals to C2 instead of C2 equals to A2 +
B2. This problem is just an order problem. You better don’t forget this rule!!
Always add first the cell you want to be changed, then add the values that
you’ll need. Like this:
Range("C2") = Range("A2") + Range("B2")

Run it again with the same green triangle.


OK! It works!

If you see a yellow line which doesn’t let you run it, just click stop, correct
your code and run it again.

Now you’ll see that in the cell C2 we find the result of A2+B2.

Let’s complete the other modules repeating the correct process above. Add
these codes to do that:

Minus Module:
Public Sub Minus()
Range("C2") = Range("A2") - Range("B2")
End Sub

Division Module:

Public Sub Division()


Range("C2") = Range("A2") / Range("B2")
End Sub

Times Module:

Public Sub Times()


Range("C2") = Range("A2") * Range("B2")
End Sub

You’re supposed to see a file like this:


All the modules should work if you run them.

Now, let’s link the Modules to their appropiate button.

To do that just do the following:

1. Go to the spreadsheet
2. Click on Design Mode
3. Double click each button. You’ll see that every time you do it, it adds
some code to the Sheet1(Sheet1). Finally, it should look like this:
Other documents randomly have
different content
procession stopped and Herr Feland lifted first his wife and Fräulein
Hohlweg, then the children, from the horses.
Rita had hardly touched the ground when she ran to and fro for
joy, and did not know which was the most beautiful, the tiny wooden
cottage with the little bench in front of the door, the green fields
around with the flowers and brooks, or the golden evening sunshine
on the rocks and fir-trees. Everything was so new, so lovely! Ella,
too, was quite filled with admiration, and looked around in silent
astonishment.
Then their father and mother came into the cottage, and a new
pleasure began for Rita, since everything here was so different from
anything she had ever seen in her life before. She seized Ella by the
hand and ran with her into every corner.
"See, see, there are seats all around the room against the wall,
and just see where you can climb up."
Whereupon Rita ran quickly up the stairs, leading up behind the
oven, to an opening through which the sleeping-room was entered.
This was a wonderful discovery! From there they went through an
open door into another chamber, where two beds stood. This led
into a little garret room and a wooden staircase on the other side
went down again into the living-room. This made a wonderful circuit
which could be made many times a day, and everything about the
whole house, inside and out, looked so new and unusual and
promised so much Rita didn't know what she should enjoy the most.
When, at last, she lay in her big bed upstairs in the chamber,
and Ella in the one beside her, and their mother had said good-night
to the children after their evening prayer, Rita drew a deep sigh and
said with the greatest contentment:
"Oh, now we are on the Gemmi!"
The most beautiful Summer days now followed, with golden
sunshine on the meadows, with cool breezes blowing up in the
evergreen woods, and the deep blue sky, spread out above the rocks
and the white, snow-capped mountains.
In a few days Ella and Rita had discovered all the lovely spots in
the neighborhood, where they could lie down and spend the warm
afternoon hours agreeably until evening, when a stroll was taken
with their papa and mamma. But Rita was more inclined to discover
lovely spots than to rest, and while Ella was sitting on the soft moss
under the fir-trees or on the green meadow ground of the mountain
side enjoying the thought that Fräulein Hohlweg was coming to read
her a charming story or tell one, Rita had always some new plan
which she proceeded to carry out.
Meanwhile their mother sat in the house with their papa, and
often had to lie down to rest, for her health was very frail.
When Rita saw Fräulein Hohlweg come out of the house with a
big basket filled with knitting materials on her arm all kinds of
delightful places immediately came to her mind, where they could
go, and before Fräulein Hohlweg had seated herself Rita would tell
her that she must go in right away to her papa, for she had a great
deal to say to him. In a twinkling she was in the house, had jumped
up on her papa's knee and was telling him a multitude of plans,—
how they could climb to the fir-trees high up on the rocks and see
far around, or go deep, deep into the woods, until they came to the
big birds that often screamed so frightfully. Papa would listen to her
daring proposals with interest, but thought there were shorter
excursions to take nearby and then would send her back to Ella and
the Fräulein.
Rita had just come to her father's knee again. To-day she had a
new proposal to make and it was very urgent.
"Oh, Papa, lay down your book for just a little minute," she
begged, "I have something to tell you."
Her papa granted her request and listened attentively.
"See, Papa," continued Rita, "yesterday and again to-day, a little
boy has been standing in front of the cottage, over there, and he
opens his eyes wide and keeps looking over here. I must really go
over there and ask him why he does that and what his name is."
Papa agreed to this necessary errand, and Rita started off at
once. Seppli had been standing over there in the same spot for an
hour, gazing at the neighbor's house opposite, for since the strange
people had come there was always something new and remarkable
to see.
When Rita reached him she placed herself directly in front of
him, and put her hands behind her, just as her papa did when he
had something important to talk over with her mamma.
"What do you expect to see that you keep looking over there?"
she asked.
"Nothing," replied Seppli.
This answer didn't seem quite satisfactory to Rita.
"Did you think we had a little boy, too, and did you want to see
how he looked?" she continued.
"No," replied Seppli curtly.
"Perhaps you have forgotten what you expected to see," said
Rita then, in order to explain the matter to herself and to Seppli.
"What is your name?"
"Seppli."
"How old are you?"
"Don't know."
"You must know. Come, stand beside me so——" and Rita
placed herself beside Seppli, and looked at him over her shoulder.
He was a little shorter, but much more stoutly built than Rita.
"You are not so tall as I am," she said. "You are quite small. You
see I shall be seven years old, for I was six years old on my
birthday; that I know very well, because I had many presents.
Perhaps you are six years old, because you are so small."
Seppli took this information without any doubt, for he did not
know that he had been seven years old some time ago, and that he
had grown more in breadth than in height.
"What do you do all day long, Seppli?" asked Rita.
Seppli had to think about it a long time. Finally he said:
"I know where there are red flowers."
This word fell like a burning spark into Rita's heart. Suddenly
she saw a bush with flaming red roses somewhere in the woods, and
everything in her longed for the wonderful flowers.
"COME, LET US GO THERE QUICKLY!"
"Where, where? Seppli, where are the flowers? Come, let us go
there quickly!" and Rita had already seized Seppli's hand and drawn
him along. But Seppli followed rather slowly.
"There," he said, and pointed with his finger to the woods
above.
"Oh, can you go there into the big forest?" said Rita expectantly,
pulling Seppli along with all her might.
"Yes, and then still farther," replied Seppli deliberately and
without hurrying his steps; he had heavy wooden shoes on his feet.
But Rita pulled Seppli still harder. She already saw the path
through the dark woods before her, and behind the trees the big red
flowers glowing and shimmering.
"Come, Seppli, come," she cried, and pulled him along still
harder.
They now came past Kaspar's cottage. Her papa was standing in
the doorway. He was looking to see why his little girl stayed away so
long, for the visit granted must have come to an end by this time.
Just as he stepped on the threshold the strange pair came along,
Rita pulling Seppli after her with all her might.
"Here, here! Not so fast, little Grasshopper!" called her papa.
"Come here! Where are you dragging your new friend?"
"Oh, Papa," cried Rita in great eagerness, "he knows where
there are such beautiful red flowers in the forest; we are going to
get them."
"No, no," said papa, taking Rita by the hand, "that won't do. We
are going to walk with mamma now, and your little friend can get
the flowers and bring them to you, then he shall have a nice piece of
bread and butter."
Whereupon her papa led his child into the house and soon they
all came out together again, father and mother, Fräulein Hohlweg,
Ella and Rita, and walked along the sunlit mountain path down
towards the valley.
Seppli remained standing in the same place until he could see
nothing more of the company; then he turned around and went back
to his own house.

CHAPTER FOURTH
A TERRIBLE NIGHT

On the following day, about the time when Frau Feland had to take
her usual rest, Fräulein Hohlweg came with her big basket to a
lovely, shady spot near the house, to spend the pleasant hour
knitting and reading. Ella sat quietly on her mossy seat, and Rita
stood in front of her, telling with great enthusiasm about a bush in
the woods, with flaming red flowers, which shone far away through
the trees. Her eyes grew bigger and brighter every moment, for the
more she talked about it the more plainly she saw it all before her,
and it seemed as if she was already on the path in the midst of the
woods.
Fräulein Hohlweg put aside her big basket and said:
"Sit down now, Rita, and be quiet. I have something lovely to
read to you."
But Rita was so full of her flowers and the woods, and all the
things she saw before her eyes, that the command was forgotten.
"I must go right away to Papa. I have so much to tell him!"
protested Rita, and ran to the house. It was the same thing that
happened every day. Rita always thought of something very
important to tell her papa, when she should have been sitting down.
To-day she had something even more urgent than usual. When a
long time had passed and the child did not return, Fräulein Hohlweg
became uneasy and said:
"Go in quickly, Ella, and call Rita, so that she will not wake
Mamma. Papa must have gone already, for he said at the table that
he was going for a long walk."
Ella ran in, but did not come back for so long that Fräulein
Hohlweg went in too. It was perfectly still in the house. No one was
in the living-room, no one in the kitchen! Fräulein went up the little
staircase and softly opened the door of the children's room. No one
was there! Through the open door she could see into the parents'
room. Frau Feland was lying with closed eyes on her bed; she was
alone in the room.
Fräulein Hohlweg came out again. Then Ella came up from
below and told her she had searched for Rita in the whole house, in
every corner, at last even in the yard back of the wood and in the
little room belonging to Kaspar's wife, but Rita was nowhere to be
found.
Fräulein ran down the stairs to the shed; there she got some
information. Kaspar's wife was standing inside spreading the straw
for the goats. When questioned about little Rita, she replied only
that she had seen the child come into the house not long before. But
where could Rita have gone afterwards? Fräulein Hohlweg and Ella
began to search through the whole house once more, then all
around it in every nook and corner. Kaspar's wife helped willingly for
she saw that Fräulein felt a real anxiety; but nowhere was there any
trace of the child to be seen. Kaspar's wife ran over to the neighbor's
house, perhaps they had seen Rita, but no one was there, the door
was closed, everything still. Then it came to the woman's mind that
Martin was making hay to-day, high up on the rocks, and that the
whole household had gone with him. She came back with this
information. Fräulein Hohlweg was usually of a timid nature, and
now she became more and more uneasy.
"Oh, if I had only gone after the child right away!" she
exclaimed regretfully a hundred times, but this was of no use. What
was to be done? Where should they look for Rita? Could she,
perhaps, have gone after the people up to the rocks, with the little
boy, with whom she had been seen the day before? The more she
thought about this the more likely it seemed to her. If only there was
someone to send up there immediately, she thought, before her
mother had to be told about the matter.
The obliging woman offered to do this and to came back again
as soon as possible, but it was a long and toilsome way; it would
take more time than one would think from looking up there.
Fräulein Hohlweg promised her a handsome reward if she would
only go and prevent Frau Feland from being frightened, and she was
very hopeful that she would surely bring Rita back home with her.
But the way was farther than Fräulein had thought, and long before
the messenger could return Frau Feland came down from her room
and wished to take a walk with the children. Then everything had to
be told her.
At the first great shock the mother wanted to go out herself at
once, to look for the child and see where she could be, but Fräulein
was so sure that Rita must have run off with the little boy that Frau
Feland calmed herself and decided to wait for the return of Kaspar's
wife. She really didn't have a peaceful moment. She ran from one
window to the other then back to the door, and then around the
house. The time seemed so long to her,—so long!
At last, after two weary hours, the woman came back, panting
and glowing from the heat, but—she came alone, without Rita.
Martin had gone up to the rocks, with his whole family early in the
morning, to make hay, and had remained there. No one had seen
the child since the day before. Moreover, along the way the woman
had asked for her, here and there, but no trace of her was to be
found.
Then the mother broke out into loud lament.
"Oh, if only my husband were here!" she cried. "Where shall we
find people to hunt for the child? What must we do? Kind woman,
what can we do?"
The woman offered to run around in the huts and summon the
people to start out to search before it should be dark; they would
have to go up along by the forest-brook, and into the forest.
"If only they hadn't all gone up to make hay," she complained,
but she started off immediately. Ella, who now realized what might
have happened to Rita, began to weep bitterly.
"Oh, Mamma, if Rita has fallen into the brook, which roars so
frightfully, or if she went into the woods and can't find her way!" she
sobbed. "Oh, let us go right into the woods. She will surely be so
frightened!"
These were also the mother's thoughts. She took Ella by the
hand and hurried up to the woods, faster than she was able to go at
ordinary times. Fräulein Hohlweg ran behind her, for she hardly
knew what she was doing she was so anxious.
One hour after another passed. Women and children ran,
searching everywhere, but no trace of Rita was discovered. Night
came on.
Frau Feland, all the while holding fast to Ella's hand, had been
running in every direction through thickets and underbrush, until
now she could run no more. She returned with Ella to the house and
fell, completely exhausted. Fräulein Hohlweg, who had followed in
her footsteps, stood breathless, looking as if she too were near
collapsing. Ella sat still, weeping, beside her mother.
Then Herr Feland came back. When he learned in a few words
from his wife what had happened, he first of all carried her up to her
sleeping-room and told her to remain perfectly quiet, that he would
do everything to find the child. Fräulein Hohlweg and Ella, he said,
must go to bed. As soon as he had found Rita he would let them
know.
Then Herr Feland went over to Martin's cottage, for his first
thought, too, was that Rita had gone away with her new friend of
the day before. Martin was just coming out of the door. He had
already heard that a child was lost and was just coming to try to
help. To Herr Feland's questions he replied how, since early in the
morning, he had been away with his wife and children, and that the
little girl had not been seen at all by them.
Herr Feland now thought Rita must have gone away alone,
either as she had proposed to him, somewhere up on the rocks, or
deep into the forest. So he ordered Martin immediately to get
together all the men in the neighborhood, provide them with good
lanterns, and have some of them climb up to the high cliffs and hunt
around everywhere and others go through the woods in every
direction. These last Herr Feland himself would join, and he was
determined to continue the search until the child was found.
So the men started off into the night, and Frau Feland heard
one hour after another strike on the old wall-clock downstairs, but
the night passed away more slowly, more lingeringly than any she
had watched through in all her life. She did not close her eyes. At
every distant sound that fell on her ear she jumped up and said to
herself:
"Now they are coming and bringing the child! But will she be
alive or dead?"
But they did not come. From time to time Ella would come tip-
toeing in softly. She wanted to see if her mother was asleep, for
through her anxiety she could find no rest either. When she found
that her mother also was awake, she would ask again and again:
"Oh, Mamma, shall we not pray once more that the dear Lord
will take care of Rita and bring her home again soon?"
Her mother assented willingly each time, and then Ella would
kneel down beside her bed and pray and beseech the dear Lord to
protect Rita from all harm and to show her papa the way to her.
Then Ella would go back quietly to her room.
The night passed. The beaming sun was already rising behind
the mountains and lighted up the woods and meadows, as if it had
great joy to announce.
Frau Feland sank back exhausted on her pillow. Finally
weariness overcame trouble and care. A quiet slumber snatched the
anxious mother away for a short time from torturing suspense and
waiting.

CHAPTER FIFTH
THE NEXT MORNING

Pale and worried, Herr Feland came through the golden morning
light back to his house, and his clothes showed that he had pressed
through many thorns and prickly briers. Frau Feland had immediately
heard his footsteps and full of anguish called:
"Are you bringing the child?"
He stepped nearer, sat down by the bed, laid his head in his
hands and said, almost inaudibly:
"I come alone. I can no longer hope, no longer think. In what
condition shall we find the child after the long night, wholly or half
dead?"
"Oh, no, Papa," sobbed Ella, who had come in softly, "the dear
Lord has surely taken care of our Rita, for Mamma and I have
prayed to him so many times in the night about it."
Her father rose. "We have gone through the forest in every
direction all night long; the child cannot be there. Now we will go
down through the ravine by the forest-brook."
Her father spoke these words in a trembling voice. The
supposition that the child had fallen into the wild forest-brook
became more and more certain to him. Herr Feland had arranged for
a good breakfast to be prepared for the men at Martin's house, and
then they were all to help further in the search. Now it was plain
that they would be better able to climb down into the ravines and
gorges.
When Herr Feland entered Martin's house the men were still
sitting at the table and talking excitedly about what to do next.
Seppli was standing by his father staring with open eyes and mouth.
Herr Feland sat down beside Martin. A silence ensued, for they
all saw what great anguish and trouble were in his heart. Suddenly
Seppli said bluntly:
"I know where she is."
"Don't talk such nonsense, Seppli," said his father reprovingly in
his gentle way, "you were up in the hay-field when she was lost; you
can't know anything about it."
Herr Feland asked for ropes and other necessary things, and
while these were being made ready Seppli said half-aloud but quite
distinctly:
"But I really know where she is."
Herr Feland rose, seized him by the hand, and said kindly:
"Little boy, look at me, and tell me truly, do you know anything
about the child?"
"Yes," was the short answer.
"Then speak out, little boy! Have you seen the child? Where has
she gone?" asked Herr Feland with increasing excitement.
"I will show you," replied Seppli, and went to the door. They all
rose. They all looked at one another. No one knew whether to take
the suggestion seriously or as foolish.
But Herr Feland followed the boy without any hesitation.
"Seppli, Seppli," said Father Martin reprovingly, "I really think
you are making a promise you can't keep."
But Seppli kept trotting along, Herr Feland following, and the
men coming reluctantly after.
When the little fellow aimed for the forest they stood still, and
one of them said:
"It is utterly useless to follow the boy in there, for we have
searched through every place and found nothing. We will not go."
Martin informed Herr Feland that he himself did not have any
confidence in the boy. But Seppli kept marching along, and Herr
Feland and Martin decided to follow.
Seppli walked resolutely on farther into the woods. Suddenly he
turned to the left toward the old fir-trees, where they soon saw
something red gleaming through. Seppli steered straight ahead,
through the midst of briers and prickly thistles, to a light spot, where
there were many large bushes together, all covered with red flowers.
Here he stood still and looked around a little puzzled. He had
evidently expected to find Rita there. Then he went with
determination on his way. The blossoming bushes became fewer, but
larger and larger. Seppli stood still by each one for a moment and
looked around, then he would go on, always to the left.
"No, Seppli, don't go any farther," cried his father. "We are
coming to the big wall of rock."
But at the same moment there was a shining like fire through
the trees. The sun glowed on a bush completely covered with the
red flowers. Seppli ran up to it quickly, but he was close to the wall
of rock, which extended, rugged and steep, down to the deep
precipice below. Seppli looked around and across the flowers down
over the rocks. Then he turned around. Herr Feland stood hopeless
behind him. The path came to an end, and the child was not found!
Martin seized the boy by the hand and tried to draw him back
from the dangerous spot, when Seppli said in his dry way:
"She is lying down there below."
Herr Feland rushed forward and bent over the precipice—his
face grew deathly pale. He stepped back and had to cling to the
nearest tree, his knees were shaking so. He beckoned to Martin,
who was still holding Seppli fast by the hand. Then he stepped to
the edge and looked down into the depths. Here and there a few
bushes hung over the precipice. In one place, horribly low down, the
rock had one small projection, like a narrow shelf. Here lay, nestled
on the rock, a motionless little being, with her face pressed against
the stone.
"God in Heaven, it is true, there she lies!" said Martin
shuddering, "but whether living or——"
He did not finish the sentence. One look at Herr Feland closed
his lips. He looked as if he were going to drop dead. But he
recovered himself.
"Martin," he said faintly, "no time is to be lost. If the child
moves she will be over the precipice. Who will climb down? Who will
get her?"
The other men now came along: hopeless, they had followed
their little guide through curiosity. They too now looked, one after
another, down the wall of rock.
"Listen, you men," said Herr Feland in a trembling voice, "there
is not a moment to lose. Who will do it? Who will help? Who dares to
go?"
The men looked at one another, but all remained silent. One of
them stepped to the edge, looked down, then turned around,
shrugged his shoulders, and went away.
"If we were only sure that she is still alive," said another. "But a
man risks his life and perhaps only to bring back a dead child."
"Who knows that she is not alive?" cried Herr Feland, almost
beside himself, "and if she stirs she is lost beyond recovery! Oh, is it
not possible?"
"She would have gone down below long before this if she was
still alive. No one could lie as still as that," said another. "And, sir, if
one should roll down there, the best reward would be of no use."
Shrugging their shoulders, one after another stepped back. Herr
Feland looked around him in despair. There was no prospect of help.
"I will do it myself," he exclaimed, beside himself; "only tell me
how?"
Martin now stepped up to him.
"No, sir," he said quietly, "that will not do. Then both would be
lost, that is sure. But I will do it, with God's help. I, too, have such
little ones, and I know how hard it must be for Herr Feland."
Even before he spoke he had fastened the big rope around the
trunk of the old fir-tree, for he had decided to bring up the child to
her father, whether she was dead or alive. Then he took off his cap,
prayed softly, seized firm hold of the rope, and slid down the rock.
He reached the little shelf in the rock. With one hand he held to
the rope with all his strength, with his bare feet he tried to cling fast
to the rock, in order to be able to seize the child with his other hand
and lift her up. Gently, quietly, he drew near, for if the child was alive
and should be startled by him—just a quick movement—even at the
last moment she would be lost.
She lay motionless there. Martin bent over the child and laid his
broad, strong hand on her. At the same moment she was about to
turn around quickly and would have fallen down beyond recovery,
but Martin's hand lay firmly on her. She could turn her head. A pair
of big, wondering eyes looked up at the man.
"God be praised and thanked!" said Martin, taking a deep
breath. "Say the same, little one, if you can still speak!"
"Yes, I can still speak! God be praised and thanked!" said the
child, in a quite clear voice.
Martin looked in greatest amazement at the child, who was
wholly unharmed.
"You must be strangely dear to our Lord, for he has worked a
miracle for you. You must never forget it all your life long, little one,"
he said thoughtfully. Then he lifted the child with his strong right
hand up to himself.
"There, now you must put both your arms around my neck, very
tight, as if I were your dear papa, for you see, I cannot hold you. I
have enough to do, with both my hands, to climb up."
"Yes, yes, I will hold fast," said Rita assuringly and clasping
Martin so firmly that he could hardly breathe. But how glad he was!
He now began to climb up the rock. It was no easy task. The
blood ran down from his hands and feet. Occasionally he had to rest
for a moment. Above stood Herr Feland and the men holding their
breath and watching the man sway above the precipice. Would his
endurance hold out? Would he come up? Or would he lose his
strength? Would he slip and fall with the child into the dark abyss?
Nearer and nearer they came—now only the last frightful steep
piece of rock—there——
"God be thanked!" cried Martin, breathless, when he took the
last step over the edge. He took the child from his neck and laid her
in her trembling father's arms.
Herr Feland had to sit down. He held his child and looked at her,
speechless, as if he could not realize his good fortune.
"Oh, Papa, I am so glad," said Rita, throwing both arms around
his neck affectionately. "I knew you would surely come to get me in
the morning."
Martin stepped aside, with folded hands; he was gazing at the
father and his child, and for joy the tears fell down over his sun-
burned cheeks. Seppli had pressed close to him and clung to him
fast, for he had realized that his father had been in great danger.
Then Herr Feland, with his child in his arms, stepped up to
Martin. He held out his hand to the rescuer.
"You know very well, Martin, that I am now doing what I should
have done before anything else," he said in a trembling voice. "I
thank you, as only one can thank another, to whom a life has been
given back. I shall never forget that you risked your life to save my
child."
The two men shook hands, and Martin said sincerely:
"It is a great reward to me that I was able to bring back your
little girl to you unharmed."
"I will see you again to-day. Now we must go to the mother,"
said Herr Feland, and, holding his little girl fast in his arms, he
started on the way back. Martin, holding Seppli by the hand, and the
others followed.
As they were going along in this way through the woods, Martin
said to his little boy:
"Now tell me, Seppli, how you knew that the little girl had come
just here?"
"Because she wanted to go to the red flowers," replied Seppli.
"But how did you know then that she could be right there by
the rock?"
"Because she was not by the first bush, so she must have gone
farther, because the flowers keep getting more and more beautiful,
and the most beautiful bush of all is the last near the rock. But I
didn't know that she had fallen off," explained Seppli.
Herr Feland now reached his house. He went in and opened the
door of the sleeping-room. Ella was still sitting by the bed and
holding her mother's hand fast. Quite exhausted, she was leaning
her head on the pillow and her eyes were closed. Herr Feland
stepped up to her mother and placed Rita in the middle of her bed.
"Good-morning, Mamma! Did you sleep well?" said Rita quite
gayly, as she did every morning when she came to kiss her mother.
Her mother opened her eyes and stared at her child. Then she
suddenly seized her in her arms, pressed her with all her might to
her breast, and tears of unspeakable joy streamed from her eyes.
She couldn't speak a word, could only thank the dear Lord again and
again in her heart.
Ella held her little sister's hand fast and kept saying over and
over:
"Are you back again, Rita? Where were you all night long
alone?"
Little by little her father told how and where he had found Rita
and how Martin risked his life to save the child. The mother
shuddered at the description. She pressed the child again close to
her when she realized the danger she had been in the whole night
long.
"Oh, weren't you frightened almost to death?" asked Ella, who
from sympathy was still struggling with her tears.
"Oh no, I was not frightened," asserted Rita gayly. "I will tell
you now how it happened. At first I was going in to ask Papa if I
might go with Seppli for the red flowers, but he was away. So I
thought he would surely allow me, because I had wanted so much
to go the day before, and then did not dare, so I went for Seppli,
but he was away too. Then I thought I would find the red flowers
alone, for Seppli had told me the way to go there."
"MARTIN BENT OVER THE CHILD AND LAID HIS
BROAD, STRONG HAND ON HER."

"Then I went up into the woods and hunted a long, long time
without finding them. But suddenly I saw something red shining
behind the trees and I ran toward it. At first there were only a few
flowers and not very bright red ones, but Seppli had said you had to
go farther and farther into the woods. So I went still farther, and
there were more and more flowers, and at last I came to a big, big
bush with so many beautiful red flowers. They shone so wonderfully
and I wanted them all, every one, and then suddenly I fell down and
rested on a stone, but it was a small stone and so I pushed back
against the rock and thought I would just lie still and Papa would
soon come and get me. But then I was tired—and it was already
getting rather dark—and I thought, I must really go to sleep, and in
the morning Papa would come and get me. Then I thought I must
say my prayer, so that the dear Lord would send his little angels to
take care of me while I slept, and I prayed:

"Oh, gentle Jesus, hear me!


On bright wings hover near me,
And keep me from all harm!
Thru danger, pain and sorrow
I'll sleep until the morrow,
Protected by thine arm.
"Then I slept very well, until a man came, and I knew right away
that Papa had sent him."
Her mother trembled as she followed the story. Her father could
not conceal his delight at it.
"Now my little Grasshopper doesn't go another step alone," he
said in as severe a tone as in his delight he could find it in his heart
to use.
The mother had not yet heard who had finally taken the
searching party to the right spot, and she wanted to know all about
it. Then the father thought of Seppli and that he was really the first
one to trace Rita.
"We must especially reward the brave boy," he said, and Rita,
who grasped this idea with enthusiasm immediately scrambled down
from the bed, in order to carry out the plan at once.
But what should the reward for Seppli be? What could she take
to him right away?
"He shall for once have his greatest wish," said her father. "We
will see what will most delight his heart."
"Can I go to him right away?" asked Rita eagerly.
Her papa wished to go with her, to speak with Father Martin at
once, and also to recompense the other men. Rita jumped all around
the room for joy. She was full of great gratitude to Seppli.
"But, Papa, supposing he should wish for a menagerie, with the
biggest animals there are?" she asked.
"Then he shall have it," was the decided answer.
"But, Papa," she asked again, "if he should wish for a Turkish
costume and a curved saber besides, such as Cousin Karl has?"
"He shall have that too!" was the answer.
"But, Papa," she went on, "if he should want a whole big
fortress and twelve boxes full of soldiers, as Karl has?"
"He shall have them!" replied her father again.
Then Rita rushed out to Seppli, who was standing in front of the
door.
"Come, Seppli," she cried, "now you can have the very best
wish you can think of!"
Seppli looked at Rita with wrinkled brow. It seemed as if her
words had awakened something that lay heavy on his heart. Finally
he said, quite cast down:
"It's no use."
"Yes, really, it is, too," replied Rita, "because you found me you
can ask for anything you would like, and you will have it. Papa said
so. Now think right away about it and then tell what it is."
Gradually Seppli seemed to understand the matter. He looked at
Rita once more to prove whether she was really in earnest or not,
then he took a deep breath and said:
"A whip with a yellow lash."
"No, Seppli, that is nothing at all," replied Rita quite vexed. "You
mustn't wish for anything like that. Think once more what is the
most beautiful thing of all and wish for that."
Seppli thought obediently, took another deep breath, and said:
"A whip with a yellow lash."
Herr Feland then came with the men out of the house. The men
went away with many expressions of gratitude, but Martin remained
standing in the doorway.
"I have not yet given you any reward, Martin," said Herr Feland.
"To you above all the rest I must prove my gratitude in a way to give
you a real joy. Tell me, have you some special desire?"
Martin turned his cap around for a while in his hands, then said
hesitatingly:
"I have had a great desire for a long time, but I dare not tell
you what it is; no, no, it should not have come into my mind."
"Speak it out fully," said Herr Feland encouragingly, "perhaps I
can help you."
"I have always thought," continued Martin, hesitatingly, "if I
could only get on as well as my neighbor over there, I would venture
to think of buying a cow. I have quite a good deal of hay and then
could take care of my family without any anxiety."
"That is good, Martin," said Herr Feland, "we shall see each
other again." Then he took Rita by the hand and started with her on
the way back.
"And what did your friend Seppli wish for?" he asked.
"Oh, he is stupid," exclaimed Rita. "He only wants a whip with a
yellow lash! That is nothing at all."
"Surely that is something," asserted her papa. "You see, every
child has his own pleasures: to Seppli such a whip would give
exactly as much pleasure as the most beautiful doll-house would to
you."
At this explanation Rita seemed contented and could hardly wait
for the wishes to come true.
On the following day Herr Feland had to make a journey down
into the valley. Rita knew very well why, and hopped with delight all
the morning long. Her papa did not go, without impressing it upon
his little "grasshopper" that she must not take a step alone away
from the house, and Fräulein Hohlweg received strict instructions.
But she had endured such anguish that terrible night that the
warnings were unnecessary. On the contrary she had determined
from now on not to take her eyes off from Rita, no matter how hard
it might be.
Two days later, when Martin had just sat down with his family at
the table to their meal of steaming potatoes, they heard a loud
mooing in front of the house—then again, and then a third time!
"Kaspar's cow must have run away," said Martin, getting up to
go and catch her. Seppli had to go too! He ran hurriedly after his
father, Martheli, Friedli and Bertheli followed, and behind them their
mother, in order to fetch them all back again.
Outside Father Martin was standing in motionless astonishment,
and all the others beside him opened their eyes wide. The mother,
who had just come along, clapped her hands and couldn't speak a
word from amazement. Fastened near the house stood a glossy
brown cow, so big and splendid, such as was only seen occasionally
among the rich peasants. To one of her horns was fastened a big
whip, which had a strong, white, leather mesh with a thick, silk lash
which shimmered in the sun like gold!
A paper was bound around the whip-handle and on this was
written in large letters: "For Seppli."
Martin took down the whip and gave it to the boy.
"It is yours," he said.
Seppli held the whip in his hand. The most beautiful and the
most wonderful thing he could think of was his very own! And,
besides, there was the cow, which could be driven up on the
mountain, with the whip to crack, like Georgie's and Chappi's!
Seppli, with beaming eyes, seized his whip, hugged it and held
it fast, as if to say:
"No power on earth can take it from me!"
Martin and his wife couldn't look enough at the splendid animal.
That it was to belong to them appeared to them like a miracle.
Finally Martin said:
"She moos because she wants to give her milk. Seppli, bring the
pail; to-day we will enjoy ourselves."
Two large pails were filled with foaming, fresh milk and placed
with the potatoes on the table; then all accompanied the brown cow
in a triumphant procession to the shed.
Over in front of the neighboring cottage Herr Feland was
standing with his children. They wanted to see how the brown cow
was received, and Rita had, above all, to know what impression the
whip would make, which she herself had marked with the big letters:
"For Seppli."
When Frau Feland had recovered from the great excitement the
whole family went up to the wall of rock in order to give praise and
thanks from their full hearts once more to the dear Lord, on the very
spot where he had so evidently spread his protecting hand over their
child.

* * * * *

THE "DOROTHY BROOKE" BOOKS


By FRANCES CAMPBELL SPARHAWK
DOROTHY BROOKE'S SCHOOL DAYS
DOROTHY BROOKE'S VACATION
DOROTHY BROOKE'S EXPERIMENTS
DOROTHY BROOKE AT RIDGEMORE
DOROTHY BROOKE ACROSS THE SEA

Each, volume, cloth, 8vo,


Illustrations by Frank T. Merrill

THOMAS Y. CROWELL COMPANY


Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about books and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!

ebookball.com

You might also like