0% found this document useful (0 votes)
11 views18 pages

Learning Unit 4

This document is a learning unit focused on VBA programming, specifically on selection and loop structures. It covers operators, selection programming structures such as IF and SELECT CASE statements, and loop programming structures including FOR NEXT, DO WHILE, and DO UNTIL loops. The unit includes activities for practical application of these concepts to automate business tasks using VBA.

Uploaded by

rothman.jg1
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
0% found this document useful (0 votes)
11 views18 pages

Learning Unit 4

This document is a learning unit focused on VBA programming, specifically on selection and loop structures. It covers operators, selection programming structures such as IF and SELECT CASE statements, and loop programming structures including FOR NEXT, DO WHILE, and DO UNTIL loops. The unit includes activities for practical application of these concepts to automate business tasks using VBA.

Uploaded by

rothman.jg1
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/ 18

LEARNING UNIT 4

VBA – SELECTION AND LOOP PROGRAMMING STRUCTURES

4.1 INTRODUCTION, OUTCOME AND ASSESSMENT CRITERIA

4.2 OPERATORS IN VBA

4.3 SELECTION PROGRAMMING STRUCTURES

4.4 LOOP PROGRAMMING STRUCTURES

4.5 BRINGING IT ALL TOGETHER

4.6 CLOSING REMARKS

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.

We will kick off our discussion by exploring operators in VBA.

4.2 OPERATORS IN VBA

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

Please access activity 4.1 by clicking here.

End of activity

4.3 SELECTION PROGRAMMING STRUCTURES

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.

Let’s look at the IF statement first.

4.3.1 If-then-else 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.

The basic syntax for the IF_THEN construct is:

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:

Figure 4.1 If-then-else statement (Van Staden, 2022)

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

Elseif condition_2 Then

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:

Figure 4.2 If-then-elseif statement (Van Staden, 2022)

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.

Upload your Excel file (XLSM) by clicking here.

End of activity

Activity 4.3

This activity builds on activity 4.1

After consulting with Mr Amla (product manager of SA Delights), it was decided that the
remuneration for the sales representatives will be as follow:

• Minimum salary: R5 000


• Commision if Sales are lower than R200 000 = 5%
• Commision if Sales are greater than R200 001 = 7.5%

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.

Upload your Excel file (XLSM) by clicking here.

End of activity

Activity 4.4

This activity builds on activity 4.1 & 4.2

Mr Azhurradin (union reprentative) did not agree with the proposal made by Mr Mr Amla.
He suggrsts the following::

• Minimum salary: R5 000


• 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.

Upload your Excel file (XLSM) by clicking here.

End of activity

4.3.2 Select-case statement

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:

SELECT CASE testexpression

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:

Figure 4.3 Select-case statement (Van Staden, 2022)

Activity 4.5

This activity builds on activity 4.1 & 4.2

Mr Azhurradin (union reprentative) did not agree with the proposal made by Mr Mr Amla.
He suggrsts the following::

• Minimum salary: R5 000

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.

Upload your Excel file (XLSM) by clicking here.

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.

4.4 LOOP PROGRAMMING 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):

• Loop through a range of cells, working with each cell individually.


• Loop through all open workbooks (by making use of the Workbooks
collection) and do something with each one.
• Loop through all worksheets in a workbook (by making use of the Worksheets
collection) and do something with each one.
• Loop through all characters in a cell.
• Loop through all charts on a worksheet (by making use of the ChartObjects
collection) and do something with each chart.

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).

The syntax for the FOR NEXT loop is:

FOR counter = start To end

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.

Type in the following code in your sub-procedure:

Figure 4.4 For-next statement (Van Staden, 2022)

Now read the code with me.

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.

Now execute your code.

Go to Excel. You will see that Excel can indeed count to ten, as per your instruction.

Figure 4.5 For-next statement output (Van Staden, 2022)

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.

Figure 4.6 For-step-next statement output (Van Staden, 2022)

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

Close all Excel workbooks.

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.

(Hint: Operators can make this task a lot easier).

Save your Excel file as an xlsm workbook.

Please upload your workbook by clicking here.

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:

Figure 4.11 Looping through worksheets (Van Staden, 2022)

4.4.2 Do while loop

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).

The syntax for the DO WHILE loop is:

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.

4.4.3 Do until loop

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).

The syntax for the DO UNTIL loop is:

DO UNTIL condition

instructions

LOOP

Again, we are going to let Excel count to 10.

Let’s look at the code in figure 4.13:

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.

4.5 BRINGING IT ALL TOGETHER

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.

Good luck with the following activity.

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.

Please upload this workbook by clicking here.

4.6 CLOSING REMARKS

Congratulations on making it this far!!!

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.

But enough on VBA …

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.

Codecademy. 2019. Intro to Programming: Loops. [online]. Available at:


https://www.youtube.com/watch?v=wxds6MAtUQ0

Rouse, M. 2022. Operator. [online]. Available at:


https://www.techopedia.com/definition/3485/operator-programming

Schoonwinkel, H. 2022. Introduction to VBA. [online]. Available at:


https://hschoonwinkel.github.io/vbaccounting/index.html

Van Staden, J. 2022. Figure 4.1 If-then-else statement.

Van Staden, J. 2022. Figure 4.2 If-then-elseif statement.

Van Staden, J. 2022. Figure 4.3 Select-case statement.

Van Staden, J. 2022. Figure 4.4 For-next statement.

Van Staden, J. 2022. Figure 4.5 For-next statement output.

Van Staden, J. 2022. Figure 4.6 For-step-next statement output.

Van Staden, J. 2022. Figure 4.7 For-step-next statement output.

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.

Van Staden, J. 2022. Figure 4.11 Looping through worksheets.

Van Staden, J. 2022. Figure 4.12 Do while loop example.

Van Staden, J. 2022. Figure 4.13 Do until loop example.

18 | P a g e

You might also like