Learning Unit 4
Learning Unit 4
4.7 REFERENCES
1|P a g e
Open Rubric
4.1 INTRODUCTION, OUTCOME AND ASSESSMENT CRITERIA
In AIN2601, you were introduced to the macro-recorder, the VBE and coding with VBA. In
learning unit 3 sequantial programming structures were discussed in detail.
You will agree that the above would offer you some automation in the workplace but would
not be sufficient to cover more advanced automation tasks. The purpose of this learning
unit is to expand your VBA knowledge to assist you in automating most repetitive tasks. This
will include some programming concepts, but you will see that these concepts are not that
hard to master.
At the end of this learning unit, you will be able to write your own code or evaluate
someone else’s code to solve or automate a business problem by using sequential, selective,
and looping programming structures or by combining them.
When coding in any programming language you will be encountered with operators. Rouse
(2022) describes an operator as a symbol that represents an action or a process. She further
explains that these symbols were adapted from mathmetics and logic.
It sounds a lot more complicated than it is. Please watch the following video by Sigma
Coding (2018) which will discuss operators in VBA. You can watch the video by clicking here.
Activity 4.1
End of activity
In life, you will find it necessary to choose between options. An example of this is when you
approach a stop street when driving with your vehicle. If you decide to adhere to the Stop
sign (which I surely hope you do �), then you will step on the brake and your vehicle will
stop. If you decide not to stop you will not step on the break, you will not stop at the stop
street, and maybe be involved in an accident …
2|P a g e
In coding, you will also have choices which will direct your code according to a pre-set
condition. In this section, we are going to cover the two most used selection code
statements used in VBA. One is the IF statement, and the other is the SELECT CASE
statement.
Alexander and Kusleika (2016:88) state that good decision making is the key to writing
successful programs. Further, they emphasise that the IF-THEN instruction is a way to
endow VBA applications with decision capability.
If condition Then
True_instructions
Else
False_instructions
End if
Let me provide an example: You need 50% to pass this module. Let’s look at a typical IF-
THEN statement:
Looks familiar…
3|P a g e
Think back to AIN2601 …
You will remember that you have entered IF statements in cells directly. Well, now AIN3701
gives you the opportunity to do it programmatically .
Also note how I have indented the code. This is another good programming practice as it
allows a reviewer to easily read through the code.
Now we can also expand the IF-THEN statement to the IF-THEN-ELSEIF. In this case, the
syntax will be:
If condition_1 Then
True_instructions_1
True_instructions_2
Else
Default_instructions
End if
Let me illustrate this with another example. You need 50% to pass this module., but if you
get 75% you get a distinction for the module. Let’s look at a typical IF-THEN-ELSEIF
statement:
4|P a g e
Now it is your time to apply this coding knowledge in the next activity:
Activity 4.2
Ms Jansen (financial director) asks you if you can assist coding a solution for the following:
Ikhishi Likagogo has decided to appoint sales representatives for the SA Delights division.
These sales representatives will work on a commission base with a minimum salary. The
minimum salary of the sales representatives will be R5 000 monthly. Sales representatives
will receive a 10% commission on the sales they have generated in a month.
Ms Jansen requires a inputbox asking what the sales values of SA Delights was for a specific
month.
Excel should then come back and provide Mrs Jansen with the salary of the sales
representative for the month.
End of activity
Activity 4.3
After consulting with Mr Amla (product manager of SA Delights), it was decided that the
remuneration for the sales representatives will be as follow:
Ms Jansen requires a inputbox asking what the sales values of SA Delights was for a specific
month.
5|P a g e
Excel should then come back and provide Mrs Jansen with the salary of the sales
representative for the month.
End of activity
Activity 4.4
Mr Azhurradin (union reprentative) did not agree with the proposal made by Mr Mr Amla.
He suggrsts the following::
Ms Jansen requires a inputbox asking what the sales values of SA Delights was for a specific
month.
Excel should then come back and provide Mrs Jansen with the salary of the sales
representative for the month.
End of activity
The SELECT-CASE is useful for choosing among two or more options and is a good
alternative to the IF-THEN-ELSE statement (Alexander & Kusleika, 2016:88). The syntax for
the SELECT-CASE statement is as follows:
CASE expression_1
6|P a g e
Instructions_1
CASE expression_2
Instructions_2
CASE ELSE
Default_instructions
End select
Let me illustrate this with an example. You need 50% to pass this module., but if you get
75% you get a distinction for the module. Let’s look at how to apply the SELECT-CASE
statement in this example:
Activity 4.5
Mr Azhurradin (union reprentative) did not agree with the proposal made by Mr Mr Amla.
He suggrsts the following::
7|P a g e
• Commision if Sales are lower than R150 000 = 5%
• Commision if Sales are between R150 000 and R200 000= 7.5%
• Commision if Sales are between R200 001 and R300 000= 10%
• Commision if Sales are higher than R300 001 = 12.5%
Ms Jansen requires a inputbox asking what the sales values of SA Delights was for a specific
month.
Excel should then come back and provide Mrs Jansen with the salary of the sales
representative for the month. Use the Select-Case statement, and not the IF statement.
End of activity
Well done for getting this far with your VBA studies. You have learned a lot and should be
proficient at this stage in sequential structures and selection structures. Next up, we will
discuss loop structures.
The last programming structure we are going to cover is the loop programming structure.
Please watch the video by Codecademy (2019), which will explain to you what a loop is, by
clicking here.
In VBA we can use loops to do the following (Alexander & Walkenbach, 2019:155-156):
In this section, we are going to discuss three types of loop statements that can be used in
VBA. This will be the FOR NEXT loop, the DO WHILE loop and the DO UNTIL loop (Alexander
& Walkenbach, 2019:156).
8|P a g e
4.4.1 For next loop
The simplest type of loop is the For Next loop. The looping is controlled by a counter
variable, which starts at one value and stops at another value. The statements between the
FOR and the NEXT statement are the statements that get repeated in the loop (Alexander &
Walkenbach, 2019:156).
instructions
NEXT
Let me illustrate this by means of an example. I will call it, let’s learn Excel how to count to
ten. (I have told you, humans are smarter than computers �).
Open a blank Excel workbook, add a module, create a sub-procedure within the module and
put a button on Sheet1, and link the button to your sub-procedure. Now go to the VBE.
We have declared a variable called Counter_Row. As we only want Excel to count to ten, we
have chosen the Integer data type.
Now we told Excel to run the following code ten times (via the For statement).
9|P a g e
When the VBA is executed in the loop the first time, the cells that will be selected is
cells(1,1). The second time will be cells(2,1), then cells (3,1) until cells(10,1) is reached. This
is due to the Next statement in the following line, which increases the Counter_Row
variable by one.
Go to Excel. You will see that Excel can indeed count to ten, as per your instruction.
Now I will show you another neat trick. You can use a Step value to skip some counter
values in the FOR NEXT loop (Alexander & Walkenbach, 2019:156). Add Step 2 at the end of
your FOR statement, as indicated in figure 4.6.
Clear cells “A1:A10” in your Excel workbook, and then execute this code. You will receive
the following output:
10 | P a g e
Figure 4.7 For-step-next statement output (Van Staden, 2022)
We can also use looping statements to run through row and columns by making use of
nested loops. Examine the following code:
Figure 4.8 For-next statement using rows and columns (Van Staden, 2022)
Look how nicely did VBA populate rows one to ten and column one to ten, by filling the
value of the val variable by running through the rows and columns. The output of the above
code is:
11 | P a g e
Figure 4.9 For-next statement output using rows and columns (Van Staden, 2022)
Activity 4.6
Now open a new workbook and populate VBA code to provide the following output (in
figure 4.10) by making use of FOR NEXT statements to fill the values. All cells with numbers
which are exactly divisible by seven should be shaded red. You can use an IF statement to
assist you with this.
Figure 4.10 Excel output as required in Activity 4.6 (Van Staden, 2022)
End of activity
12 | P a g e
It is also possible to loop through worksheets by their order, by replacing the worksheet
number with a counter variable (Schoonwinkel, 2022). Let’s assume that our workbook has
five worksheets. The following code will insert “AIN3701” in cell “A1” in each of the
worksheets:
The second type of loop is the Do While loop. The Do While loop executes its instructions as
long as a specified condition is met (Alexander & Kusleika, 2016:101).
DO WHILE condition
instructions
LOOP
Let’s learn Excel again how to count to 10. We can also achieve this by using a DO WHILE
loop. Let’s look at the code in figure 4.12:
13 | P a g e
Figure 4.12 Do while loop example (Van Staden, 2022)
The code in the loop will run while the counter variable is equal or less than ten (do while
the counter is equal or less than ten). It is, however, important to increase the value of the
counter variable by one, in the instructions. Otherwise, the value of counter will remain one,
and the loop will run until infinity. This will cause your module to crash.
The third type of loop is the Do Until loop. The Do Until loop execute its instructions until a
specified condition is met (Alexander & Kusleika, 2016:102).
DO UNTIL condition
instructions
LOOP
14 | P a g e
Figure 4.13 Do until loop example (Van Staden, 2022)
You will notice a small difference in the code. The Do until loop will run till counter is greater
than ten. Therefore, the instructions within the loop will run ten times.
We have covered in this section all three loops. It is important to notice that you can
achieve the same results with each one of these loops. In my opinion the FOR NEXT loop is
the easiest to use, and I would also recommend that you use this loop. But there can be
circumstances in which to use the other two loops (DO WHILE & DO UNTIL).
It is, however, important that you can read and understand VBA code where any of the
three loops were used.
You have learned a lot about VBA up to this stage, and with the knowledge obtained you
should be able to automate functions using VBA. Note that in coding, there could be more
than one way to come to a solution. Therefore, adding comments to your codes are crucial.
Also, remember to save your Excel workbooks to macro-enabled workbooks (xlsm). If you
do not save your workbooks to this file format, you will lose all the VBA code you have
developed. I have lost my code a few times by not saving in the correct file format, so learn
from my mistakes �.
15 | P a g e
As I have explained in learning unit 3, to become a good VBA coder, you need to practise
this skill a lot. In the next activity, I will provide you with the business problem, and you will
need to use your gained knowledge, as a VBA coder, to solve this problem. I will therefore
not provide any guidance as to the coding solution.
Activity 4.7
You have become known in Ikhishi Likagogo as the automation expert. Ms Tau (product
manager of the Cake division) obtained the following system download when enquiring
about the sales for the past quarter. Please download the system generated report by
clicking here.
Ms Tau told you that she struggles to copy and paste the information to the sheet called
Structured. “It takes time to copy, paste and then delete the Product and Inv Nr lines.
Furthermore, sometimes I make mistakes when updating the Product in the Structured
worksheet �”
Ms Tau asks you if you could generate a VBA solution to automate this tedious copying and
pasting for her.
I hope you have learned how to automate boring repetitive functions using VBA.
Remember that you need to practise this skill to stay on top of your game, and ultimately
master it.
There are a lot of other interesting VBA areas, which will further assist you in automating
tasks in Excel. These are workbook events, forms, and active X controls. If you want to
explore these areas further, a good starting point is https://www.excel-easy.com/. Please
16 | P a g e
note that the exploration of these additional topics will only enhance your knowledge of
VBA and will not be assessed in AIN3701.
Just a comment on activity 4.7. Often, computer systems generate reports in a semi-
structured way. Therefore, human intervention is necessary to get the data in a structured
format. Activity 4.7 is a good example of how to use VBA to assist intaking semi-structured
data and changing it into structured data.
In the next learning unit, we are going to enter the world of data analytics. See you there.
4.7 REFERENCES
Alexander, A & Kusleika, D. 2022. Excel 2016 Power Programming with VBA. 1st edition. John
Wiley & Sons. Indianapolis.
Van Staden, J. 2022. Figure 4.8 For-next statement using rows and columns.
Van Staden, J. 2022. Figure 4.9 For-next statement output using rows and columns.
17 | P a g e
Van Staden, J. 2022. Figure 4.10 Excel output as required in Activity 4.6.
18 | P a g e