Activity 2 Flowchart and Excel VBA - Linear Interpolation Extrapolation PDF
Activity 2 Flowchart and Excel VBA - Linear Interpolation Extrapolation PDF
1. Overview
2. Learning Outcomes
3. Task
As a formation evaluator, your task for today is to fill-out the required porosity and
permeability data for oolitic limestones from the fictional Sta. Monica Oil-Field (Table 1).
1
PetE 31FE Formation Evaluation
3.2 Outputs
To perform the given task, you need to create an Excel VBA tool that estimates the
aforementioned properties through interpolation or extrapolation from the given graph
below (Fig. 1). You also need to provide the following:
Porosity (%)
Figure 1 Permeability vs. Porosity Plot of different sedimentary rocks (Tiab & Donaldson,
2016).
Before you can perform the task, you need to equip and train yourselves first with the
right tools and skills, respectively. You have a week to finish the task. Learn at your own
pace, and have fun learning new stuffs. Let’s
2
PetE 31FE Formation Evaluation
4. Skills
And below are examples of notes taken from the topic (Fig. 2):
a.
b.
Now, as you look at these notes, are you thinking: “Yes! Now I understand what is LCD.
Very inspiring! This will help me in my exams.” If so, good for you, but if you find both of these
notes terrible (aside from my hand-writing), you’re not the only one. Most of us find linear
note-taking tedious and uninspiring. We just copy or write down what we see and hear, then
write it all again the night before hell week because we didn’t remember and understand what
we wrote.
Luckily, there’s a way to have fun while taking-notes. A method that increases your
retention and comprehension, helps you organize and structure concepts and develops your
thinking process. This method is called Mind Mapping.
3
PetE 31FE Formation Evaluation
4
PetE 31FE Formation Evaluation
Example Topic:
“The lowest common denominator or least common denominator is the lowest common
multiple of the denominators of a set of fractions. It simplifies adding, subtracting, and
comparing fractions.” (Wikipedia)
Step 1:
- Position the main idea or concept at the center of the paper (Fig. 4).
- Use shapes or images that will help you associate to the idea.
Step 2:
If we use books as an analogy, then the main idea is the title of a book while the main
branches are the chapters. Identifying the underlying concepts can be quite difficult especially
if you’re taking notes during live discussions. It’s not advisable to write anything anywhere.
You need to be logical in structuring the concepts. You should categorize the concepts from
broad to specific (Fig. 5).
5
PetE 31FE Formation Evaluation
Tips:
Instead of writing the concepts as main branches, replace them with:
a. what, why, when, where, how.
b. or use definition, objective or purpose, procedure or method. (I personally use this
one.)
Therefore:
Step 3:
Figure 7 Subtopics
This concludes our note-taking but not our mind mapping. It may not seem obvious,
but learning from note-taking is limited by the one who provided the information. The
information can lack details, accuracy or logical structure, or the presentation is not suitable
to our own way of learning. This is where mind mapping can be helpful. It can further our
learning by converting the taken notes as our own. This is the part where we are making our
own notes through research, validation and elaboration. We can start by asking questions
like what, why and how, and answer these questions through researching, shifting our
perspectives and making analogies.
6
PetE 31FE Formation Evaluation
Step 4:
My mind map may not be creative enough for your taste, but it is enough for me to
understand and present the concept of least common denominator (LCD). The questions are
represented by red fonts and lines (Fig. 8), and a connection between two branches is
represented by a symbol ( ), which is specific to the relationship.
- I was curious if there are rules or criteria that are necessary before performing the
calculation, so I started from the definition and purpose and reviewed the concept of
multiples and fraction operations, which became the basis for the rules. I also added
detailed illustration (the purple font at the top left).
- Reviewing the fraction operations made me realize that I was only thinking about
operations between two fractions. It did not occur to me that the set of fractions can
be two or more. This also shows that sometimes you can miss the simplest of things.
- I researched about the fastest way to find the least common denominator (LCD), and
it turns out there is a corresponding method for a set of two fractions and another for
a set of three or more.
7
PetE 31FE Formation Evaluation
- I also constructed a more brute method of finding the LCD. This is based on the basic
way of finding the LCD. It is also applicable to a set of two or more fractions.
This concludes the mind mapping. Of course, I can still add more details by making
analogies or by asking who developed the methods, what is the rationale for each of the
methods, are the methods restrictive to their criteria or are there more methods available that
I did not know. As long as you are curious, you can expand your notes and your learning.
Mind mapping can improve retention, critical thinking, organization and presentation
(e.g., Buran & Filyukov, 2015; Farrand et al., 2002; Kernan et al., 2017).
It’s your turn to create your own mind maps. I believe that you can organize your
thoughts in creative ways and improve your learning. You can choose your own set of colors
and create rules on its application. You can also use lines or symbols to show relationships
and add images, links, videos or documents if you’re using an application. Always describe
the procedure in detail and use examples or analogies. Just remember, you need to be curious.
You can draw or use a software for your mind map about interpolation/ extrapolation.
You also need to include your thought process just like the example above.
4.2 Flowchart
Now, if you look at the procedure of finding the least common denominator for a set
of two fractions, we see that it starts with finding the greatest common factor (GCF). To us,
this is automatic. We don’t need to think hard about it, but for a computer, it needs a
8
PetE 31FE Formation Evaluation
systematic approach on how to do this. Computers don’t know what to think. We need to tell
them what to do. Therefore, we need to create a procedure, where the only necessary user
intervention is the input, and this can be done using programming concepts (e.g., loops).
Start
“Hello World”
End
Start/ End
Input/ Output
Conditional Statements
9
PetE 31FE Formation Evaluation
Example:
The brute method for determining the least common denominator (LCD) in Figure
8. (This method is easier to convert into an algorithm.)
- You can use a top-down or bottom-up approach. (Personally, I use the bottom-up.)
- Bottom-up is basically output to input. Start from the output data, which in this case
the LCD, and then determine the necessary processes and input data. (Top-down is
the reverse of bottom-up.)
Bottom-up:
a. Determine the process that will generate the output data and the criteria to
perform the process.
Variables: 𝑥 and 𝑖
c. Determine the process that will generate the values of 𝑥 and 𝑖 and the criteria
for each variable.
For 𝒙:
Process: Find Largest Denominator among the given denominators
𝐷1 , 𝐷2 , 𝐷3 , … , 𝐷𝑛 .
Criterion: Largest Denominator
For 𝒊:
Process: Loop
Criteria:
- Increment: +1
- Value starts from 1.
10
PetE 31FE Formation Evaluation
d. Repeat step B. If the remaining processes are all user inputs, constants and/or
loop counters (e.g., 𝑖), you are finished.
For 𝑫𝟏 , 𝑫𝟐 , 𝑫𝟑 , … , 𝑫𝒏 :
Process: Input
Criterion: Input the number of denominators.
For 𝒏:
Process: Input
Criterion: 𝑛 > 1
- Follow the algorithm from Step 1 using also the bottom-up approach.
a. Start!
Start
b. Input 𝑛.
Start
c. Check criterion of 𝑛. If it doesn’t pass the criterion, return to input or stop program.
Start
𝑛≥1 No
Yes
11
PetE 31FE Formation Evaluation
d. Input denominators. From the criterion, 𝑛 must be given before the input
denominators. That is the reason why 𝑛 is the first in the flowchart.
Start
𝑛≥1 No
Yes
𝐷1 , 𝐷2 , 𝐷3 , … , 𝐷𝑛
e. Next is the values for 𝑥 and 𝑖. The variable 𝑖 starts from 1 while 𝑥 is determined
from the denominators. Most programs have sorting functions, which makes it
easier to extract the largest or the smallest value from a data set, and therefore, we
don’t need to sort it manually. Also, the value of 𝑥 is not from the user input but
from a sorting process. This is also the same with 𝑖. Its value is defined by the
programmer but not inputted by user.
Start
𝑛≥1 No
Yes
𝐷1 , 𝐷2 , 𝐷3 , … , 𝐷𝑛
𝑖=1
Sort Denominators
𝑥 = 𝑙𝑎𝑟𝑔𝑒𝑠𝑡 𝑑𝑒𝑛𝑜𝑚𝑖𝑛𝑎𝑡𝑜𝑟
12
PetE 31FE Formation Evaluation
f. Calculating the LCD. The criteria must be met first before proceeding to the LCD.
If one of the denominators did not pass, the process loops and repeats with a new
value of 𝑖. Don’t forget to terminate (i.e., End) the flowchart.
Start
𝑛≥1 No
Yes
𝐷1 , 𝐷2 , 𝐷3 , … , 𝐷𝑛
𝑖=1
Sort Denominators
𝑥 = 𝑙𝑎𝑟𝑔𝑒𝑠𝑡 𝑑𝑒𝑛𝑜𝑚𝑖𝑛𝑎𝑡𝑜𝑟
𝑥𝑖 Yes 𝑥𝑖 Yes 𝑥𝑖
= 𝑖𝑛𝑡 ≥ 1 = 𝑖𝑛𝑡 ≥ 1 = 𝑖𝑛𝑡 ≥ 1
𝐷1 𝐷2 𝐷𝑛
No No No Yes
𝑖 =𝑖+1
𝐿𝐶𝐷 = 𝑥𝑖
𝐿𝐶𝐷
End
13
PetE 31FE Formation Evaluation
You can also use the top-down approach like how recipes and procedures are created,
but you must be well-versed about the processes. The bottom-up approach will help you
effectively analyze the methods during the process of creating the algorithm, so even if you’re
still unfamiliar with some steps, using the bottom-up approach will improve your
understanding.
If you carefully observe the example, there are recurring patterns, specially between
the steps and some important programming concepts such as conditional statements and
loops. The criteria serve as the condition for conditional statements while repeating steps or
iterations are converted to loops.
It is important to test the flowchart to see if it works. The best way to do this is to use
it manually to solve a problem. Try to follow it or map your process and check if it can
generate the correct results given an input data.
Example:
1 1
Find the LCD between 2 and 3.
Check 𝑛. Is 2 ≥ 1? Start
If yes, proceed; No,
go back and input
new 𝑛. 2
2≥1 No
Yes
14
PetE 31FE Formation Evaluation
Input
Denominators. Start
2≥1 No
Yes
𝐷1 = 2
𝐷2 = 3
2≥1 No
Yes
𝐷1 = 2
𝐷2 = 3
𝑖=1
Sort Denominators (3,2)
𝑥=3
15
PetE 31FE Formation Evaluation
loop (𝑖 = 𝑖 + 1).
𝑖=1
Sort Denominators (3,2)
𝑥=3
No No No
𝑖 =𝑖+1
Yes
𝐿𝐶𝐷 = 𝑥𝑖
𝐿𝐶𝐷
End
16
PetE 31FE Formation Evaluation
No No No
𝑖 =1+1
Yes
𝐿𝐶𝐷 = 𝑥𝑖
𝐿𝐶𝐷
End
𝑖 =1+1
Yes
𝐿𝐶𝐷 = 𝑥𝑖
𝐿𝐶𝐷
End
17
PetE 31FE Formation Evaluation
𝑖 =1+1
Yes
𝐿𝐶𝐷 = 𝑥𝑖
𝐿𝐶𝐷
End
3 is the last
denominator. 3(2) Yes 3(2) Yes 𝑥𝑖
=2 = 𝑖𝑛𝑡
Therefore, we can 2
=3
3 𝐷𝑛
proceed to ≥1 ≥1 ≥1
calculate LCD.
No No No
𝑖 =1+1
Yes
𝐿𝐶𝐷 = (3)(2)
𝐿𝐶𝐷 = 6
End
𝐿𝐶𝐷 = 6
End
18
PetE 31FE Formation Evaluation
Algorithms and flowcharts are not only for computer programs. These are also
beneficial to us as engineers. The least common denominator may be elementary, but as we
peel the underlying concepts, we discovered subtle details that we don’t fully comprehend
before. It can improve the rigorousness of our methods and expose the limits and complexities
of processes. In problem-solving scenarios, it can help us solve things systematically without
thinking too much and can aid us during mind blanks.
Congratulations! You’ve learned to articulate concepts using mind maps and perform
procedures using flowcharts, but the flowcharts are still too much for our computer. Some of
the steps requires more elaboration, which can be done through programming concepts (e.g.,
loops).
You are now at the final section and the most exciting part of this training. You will
be integrating your flowcharts with Excel VBA. Let’s
19
PetE 31FE Formation Evaluation
20
PetE 31FE Formation Evaluation
Once you’ve enabled the Developer Tab, you don’t need to repeat these steps every
time you open Excel.
21
PetE 31FE Formation Evaluation
A new window will appear: The Visual Basic Editor (VBE) Window.
22
PetE 31FE Formation Evaluation
There are three options: Userform, Module and Class Module. We will write most of
our codes in the Module.
23
PetE 31FE Formation Evaluation
Step 4: Select Module among the three options (i.e., Userform, Module, Class Module).
Module 1 is created, and its corresponding code window is opened. We can also
rename Module 1 at the Properties Window or by Right-clicking Module 1 under the Module
Folder. The Code Window is where we will write our VBA syntax. We can create a lot of
modules for different processes, but it’s more ergonomic to put all our codes in one module.
Step 1: Pre-syntax
Before writing our main syntax, let’s write two important codes at the top of the code
window. First is Option Explicit. We can write it manually or enable it at the Toolbar →
Tools → Options → Editor Tab → Check Require Variable Declaration. If you do the latter,
every time you create a new module, the code is automatically written at the top. What this
code does is it prompts Excel to require us to declare all of the variables. Therefore, if a code
contains a variable that was not declared, Excel will prompt us with an error window. Last is
the Option Base 1. This is for indexing of arrays, so that the index starts at 1 instead of the
default value, which is 0. You will understand the rationale for this code in the succeeding
sections.
24
PetE 31FE Formation Evaluation
Write the name of your Sub procedure: Sub Name() (Note: No spaces in the name part.). Then
hit enter. It will automatically enclose the procedure with End Sub. You will write the code
inside the Sub procedure.
Dim statements are used to declare variables in VBA. You only need to use it once per
line and not per variable.
From the example exercise, there are 5 variables based on the flowchart:
25
PetE 31FE Formation Evaluation
All of the variables are integers because denominators are always integers. I usually
arrange my variables based on data types and separate variables that are used as loop counters
(e.g., 𝑖). For the denominators, there quantity is based on 𝑛 and is not fixed. Therefore, we
need to use a type of variable that can accommodate this – dynamic arrays. Arrays are
variables that can store a set of data in a matrix. Matrices are usually 1D or 2D and rarely
more than two dimensions. A spreadsheet typifies a matrix. We can envisage a 1D array and
2D array as a row with multiple columns and multiple rows with multiple columns,
respectively. There are two types of arrays: static arrays, which have fixed dimensions and
dynamic arrays, which have varying dimensions.
To declare a static array, you need to define its dimensions: (Option Base 1 is enabled.)
ID code 1 code 2
1 as1 qw1
2 as2 qw2
3 as3 qw3
Example Data for arr_static (Note: The headers are not included).
The example above is a 2D array that contains values with different data types.
Therefore, the array is declared as variant to accommodate all of the different types of data.
The first dimension defines the upper boundary of the index for rows (e.g., 3), and the second
dimension defines the upper boundary of the index for columns (e.g., 3). Remember the code:
Option Base 1? If this code is omitted, the index will start at zero. This means that row or
column 1 is index 0, row or column 2 is 1 and so on. Therefore, if Option Base 1 is disabled
and arr_static has 3 rows and 3 columns, the declaration should be:
26
PetE 31FE Formation Evaluation
But it’s more beneficial to use the syntax for 2D arrays even if the data is 1D:
Or
Color
red
orange
yellow
In this case, the data are contained in a 2D array even though the data is a 1D list.
You can choose how your data can be arranged unlike in the case of 1D arrays. Another
advantage is you can directly copy or paste values from or to spreadsheets, respectively, in
the same format or arrangement. We will build more intuition on arrays in the succeeding
modules.
If the dimensions of an array are initially unknown and are only characterized during
a process or after an input, then we can declare the arrays with no dimensions just like with
our example for least common denominator. The variable arr_D, which will contain the
denominators, is declared with no dimensions. This variable will be re-dimensioned after
inputting 𝑛. Dynamic arrays can always be re-dimensioned anywhere after the initial
declaration using the Redim statement. For example:
There are still a lot to cover on arrays, but we will stop the discussion here and proceed
to the next part.
27
PetE 31FE Formation Evaluation
The input from an input box is taken as a string, but we can convert it to a number
using the Val function. As you can see below, the input box is assigned to 𝑛. Therefore, 𝑛 will
get its value from the input on the input box.
You can test this code by pressing F5 or by clicking the play button at the tool bar.
If you play the code, an input box will appear, where you will place the input.
28
PetE 31FE Formation Evaluation
Instead of going back to the input if the criterion is not met, I changed it to
termination of the program. Now, there are ways to do this, but let’s use the conditional
statement for now. Therefore, if the criterion is not met, the program will not proceed to
whatever code that will be within the if-statement and will proceed to the next line, which is
the End Sub (termination). This syntax works because there are no lines after the if-statement
except the termination line. Take note of the format of the if-statement.
From the LCD flowchart, if the criterion for 𝑛 is met, the next process is to input the
denominators, but as we now know, we need to re-dimension the arr_D before we can input
any values. Therefore:
We can now input denominators, but how? In this case, we’ll use a loop, so let’s add
a new loop counter, 𝑎.
29
PetE 31FE Formation Evaluation
Loops are always partnered with arrays, especially in populating the arrays with
values. There are many types of loops in VBA (e.g., Do-While, Do-Until), but most of the
time a For-Next loop will suffice. If we look at the format, the loop counter succeeds the For
statement together with its initial value and the final value (e.g., 1 to 𝑛). This also means that
the process will loop only if 1 ≤ 𝑎 ≤ 5. The Next statement increases 𝑎 by 1 before repeating
the process.
(Note: The ampersand, &, are used to concatenates texts with other texts or numbers.)
Next, we will sort the denominators using the sorting tool in Excel. First, we need to
paste arr_D on the spreadsheet, but before we can access the parts of Excel, we need to
understand first the concept of object hierarchy.
Components in Excel (and even Excel itself) are classified as objects, and these objects
have properties, which can be read-only or read-write. Before we can access and toggle these
properties, we need to follow VBA’s object hierarchy. We can liken this system to a cabinet
with many compartments. If we use this analogy, the cabinet represents Excel. And inside it,
there are workbook compartments. We can think of it as the main compartments of the Excel
cabinet, and these workbook compartments are called the Workbooks Collection. If we want
to access a particular workbook, we need to open the Excel cabinet, and then access the
selected workbook compartment among the collections. The syntax for this is:
30
PetE 31FE Formation Evaluation
The application represents Excel, and within it, there is a Workbooks Collection.
Among the Workbooks Collection, there is a workbook called Book1.xlsx. (Note: Collections
are always plural.)
Let’s continue our cabinet analogy. Each of the workbook compartments has their
own Sheets Collection, and there are two types of sheet collections: Worksheets Collection
and Charts Collection (i.e., Chart Sheets). Let’s focus on worksheets for now. Each of the
worksheets has cells within it. These group of cells are called Range, and each of the cells has
a Value Property that we can read and write. Therefore, if we want to know the value of cell
A1 from Sheet1 inside Book1, we need to write the following:
(Note: Sheet1 and Book1 are names of a worksheet and workbook. Therefore, if the name of
your worksheet and workbook are “LCD” and “FormEval”, respectively, use these names
instead, and don’t forget the quotations.)
We can shorten this syntax into the line below if our codes only need to manipulate
the objects inside the workbook that we are working on.
Going back to our LCD example, we need to paste the values of the arr_D in the
spreadsheet. We will paste arr_D in column A starting from cell A1 inside Sheet1. The
column format is based on the format of arr_D, which is in column format. Let’s start by
clearing the values of column A whether it has values or not. To clear the values, we will use
the Clear Method. Methods of an object perform actions. Therefore: (Range(“A:A”) defines
the column A.)
31
PetE 31FE Formation Evaluation
(Note: Underscores are used to indicate that the code continues to the next line.)
The syntax for sorting contains a lot of arguments. Let’s just look at the important
ones and leave the others to their default values. The first line clears any sorting that was
applied to the data. The second line is where the sorting is enabled. The Key defines the
location of the data or table and the Order defines the order format (i.e., descending or
ascending). The next line is the SetRange, which also defines the range of the data. Next is
the Header argument, which has a default value of xlNo. Since our set of data has no header,
we’ll leave it to its default value. The last important line is the Apply.
There’s no need to memorize this syntax or any complicated syntax in VBA. You can
just search it in the web. The important thing is knowing the arguments.
If you’ve observed the syntax for sorting, there is one recurring statement,
Worksheets(“Sheet1”).Sort. We can use the With-statement to shorten the syntax:
32
PetE 31FE Formation Evaluation
Based on the sorting, the largest denominator will always be located at cell A1.
Therefore, 𝑥 will be equivalent to the value of cell A1.
Next, check 𝑥𝑖 criteria: (We’ll use a Do-Until loop together with a For-Next loop.)
As you can see, we have a nested loop, and 𝑖 is initiated before the Do-Until loop. Also,
remember that 𝑥𝑖 must be check for all denominators, which are stored in an array. To check
for each of the values in the array, we need to use the For-Next loop. The For-Next loop can’t
be used for 𝑖 since this type of loop requires a maximum value for its counter, which we can’t
define for 𝑖. Therefore, the Do-Until loop is necessary to execute the loop. The Do-Until loop
will iterate until the criteria are met.
𝑥𝑖 𝑥𝑖
There are two criteria for 𝑥𝑖: first is 𝐷 ≥ 1 and second is 𝐷 = 𝑖𝑛𝑡𝑒𝑔𝑒𝑟. Therefore, if
you look at the If-statement within the For-Next loop, there are two conditions that represent
the criteria and are linked by And, which denotes that both criteria must be met. The Int
function converts the number to an integer. If the original value divided by the converted
value is equal to one, then the number is divided by itself and is an integer. The 𝑐 is a counter
to check if the criteria were met 𝑛 times and is also used as the condition for the Do-Until
loop. Its value is reset for every iteration in the Do-Until loop. The next If-statement checks
if 𝑐 is equal to 𝑛. If 𝑐 = 𝑛, then 𝑥𝑖 is assigned to 𝐿𝐶𝐷 and the Do-Until loop is stopped.
The last step is to show the output data. This can be done by pasting the output to a
cell or range of cells or by showing it through a message box.
33
PetE 31FE Formation Evaluation
The syntax is MsgBox then followed by a text or any value that you want to show.
b. Click Insert
34
PetE 31FE Formation Evaluation
e. A pop-up will appear, and under it are your Sub procedures or macros. Select the
desired procedure, which in this case is the Find_LCD procedure, and click OK.
f. A button will appear. You can rename and resize it. To use the button, click anywhere
to exit from the edit mode, and then click the button, and it will execute the procedure
for you.
35
PetE 31FE Formation Evaluation
The VBA program for finding the least common denominator is now finished, but of
course we can still make it more error-proof such as including a set of criteria (e.g., no zero
values, decimals or letters) for the denominators or for 𝑛. The elaboration of some of the steps
where mostly done by using loops, conditional statements, arrays or other Excel functions,
and it is difficult to use these concepts without a flowchart. Therefore, you need to create a
flowchart before converting it to codes.
If you want to further your skills in Excel VBA, you can read Excel VBA for Dummies
by Alexander & Walkenbach (2019).
5. References
Alexander, M., & Walkenbach, J. (2019). Excel VBA Programming for Dummies (5th ed.).
Buran, A., & Filyukov, A. (2015). Mind Mapping Technique in Language Learning. Procedia - Social
and Behavioral Sciences(206), 215-218.
Buzan, T., & Buzan , B. (1993). The Mind Map Book: How to Use Radiant Thinking to Maximize your
Brain's Untapped Potential.
36
PetE 31FE Formation Evaluation
Farrand, P., Hussain, F., & Hennessy, E. (2002). The Efficacy Of The ‘Mind Map’ Study Technique.
Medical Education(36), 426-431.
Kernan, W., Basch, C., & Cadorett, V. (2017). Using Mind Mapping to Identify Research Topics: A
Lesson for Teaching Research Methods. Society for Public.
Rodrigues, J. (2011, August 31). Everything You Need To Know About Mind Mapping. Retrieved from
Iris: Speed Reading Classes, Memory & Productivity Courses:
https://irisreading.com/mindmap/
Tiab, D., & Donaldson, E. C. (2016). Petrophysics: Theory and Practice of Measuring Reservoir Rock
and Fluid Transport Properties (4th ed.).
37