Looping & Concatenation LECTURE 4
Looping & Concatenation LECTURE 4
Quick recap of variables For-Next loops Concatenation (&) Loops and Concatenation
SUMMARY
QUICK RECAP
Download Lecture 4 Student Example.xlsm
In Module1, find the procedure called Recap
Declare disposable variables x, y and z as integer.
Assign x a value of 5 Assign y a value of x + z Assign z a value of 10
Output the value of x to cells(1, 1) Output the value of y to cells(2, 1) Output the value of z to cells (3, 1)
EXERCISE. REVIEW OF VARIABLES
Not necessarily in this order
Declare disposable variables x, y and z as integer.
SOLUTIONS. REVIEW OF VARIABLES
Public Sub Recap() Dim x as integer, y as integer, z as integer End Sub
Assign values to each variable.
SOLUTIONS. REVIEW OF VARIABLES
Public Sub Recap() Dim x as integer, y as integer, z as integer End Sub
x = 5 z = 10 y = x + z
x = 5 y = x + z z = 10
y = x + z x = 5 z = 10
Assign values to each variable.
SOLUTIONS. REVIEW OF VARIABLES
Public Sub Recap() Dim x as integer, y as integer, z as integer x = 5 z = 10 y = x + z End Sub
Output variable values to cells.
SOLUTIONS. REVIEW OF VARIABLES
Public Sub Recap() Dim x as integer, y as integer, z as integer x = 5 z = 10 y = x + z End Sub
Cells(1, 1) = x Cells(2, 1) = y Cells(3, 1) = z
x = Cells(1, 1) y = Cells(2, 1) z = Cells(3, 1)
Output variable values to cells.
SOLUTIONS. REVIEW OF VARIABLES
STEPPING THROUGH CODE
Use the code you typed in the previous exercise
STEPPING THROUGH YOUR CODE
Position screens so you can see both at once
STEPPING THROUGH YOUR CODE
Click a start and end point for stepping through your code (it should turn red).
STEPPING THROUGH YOUR CODE
Click HERE
Click HERE
Run your code (F5) It will pause at the first dot and turn yellow Use shift+F8 to run each line of code
STEPPING THROUGH YOUR CODE
Put your cursor over variables to see their current value. Variables will only show their value AFTER the line
of code as run. If the line is yellow, it means it hasn’t run yet, but it will
run next.
HOVER OVER VARIABLES
Example: Check the value of y
Watch the output to check it is correct Use this technique for checking For-Next loops!
CHECK THE OUTPUT
This code has not run yet
FOR-NEXT LOOPS An Introduction
Enter 1 to 5 into column 1 in Excel
EXAMPLE 1. FOR-NEXT LOOPS
Public Sub ForLoopExample1() Cells(1, 1).Value = 1 Cells(2, 1).Value = 2 Cells(3, 1).Value = 3 Cells(4, 1).Value = 4 Cells(5, 1).Value = 5
End Sub
Very repetitive and inefficient
Use For-Next Loops to improve this code Identify what values are changing Identify what values are staying the same
EXAMPLE 1. FOR-NEXT LOOPS
Public Sub ForLoopExample1() Cells(1, 1).Value = 1 Cells(2, 1).Value = 2 Cells(3, 1).Value = 3 Cells(4, 1).Value = 4 Cells(5, 1).Value = 5
End Sub
These values are changing
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 1. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 1. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
When i = 1 row is 1
value is 1
When i = 1 row is i
value is i
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 1. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
When i = 2 row is 2
value is 2
When i = 2 row is i
value is i
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 1. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
When i = 3 row is 3
value is 3
When i = 3 row is i
value is i
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 1. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
For every line of code the row is i
and the value is i
i is an integer I need to declare
EXAMPLE 1. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
i is an integer I need to declare This is a For-Next Loop
EXAMPLE 1. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
i is an integer I need to declare This is a For-Next Loop i is the counter – it will increase by 1 each time The first value of i is 1 The last value of i is 5
EXAMPLE 1. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
i is an integer I need to declare This is a For-Next Loop i is the counter – it will increase by 1 each time The first value of i is 1 The last value of i is 5
EXAMPLE 1. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
This code will run for each value of i
FOR-NEXT LOOPS
Begin with FOR
End with NEXT
A counter is a letter or word declared as a disposable integer variable
FOR-NEXT LOOPS
Must specify a COUNTER
FOR-NEXT LOOPS
Values the COUNTER will take
Increases by +1 by default END value must be > than START value
Can increment by -1 if necessary to count backwards
FOR-NEXT LOOPS
Must specify START value
Must specify END value
Increases by +1 by default Can increment by -1 if necessary to count backwards
FOR-NEXT LOOPS
The result is the same for both loops
Always START with 1 or 0! You will understand why when you start coding
FOR-NEXT LOOPS
FOR-NEXT LOOPS
CODE to run goes inside the loop
Use Lecture 4 Student Example.xlsm
Write a For-Loop for this: Remember to declare your counter! Loop from 1 to 3 OR from 0 to 2 Write it out the long way first if you need to
EXERCISE 1. WRITE A FOR LOOP
Write this output to these cells
Run your code to ensure it works
Enter 6 to 10 into column 2 in Excel
EXAMPLE 2. FOR-NEXT LOOPS
Public Sub ForLoopExample2() Cells(1, 2).Value = 6 Cells(2, 2).Value = 7 Cells(3, 2).Value = 8 Cells(4, 2).Value = 9 Cells(5, 2).Value = 10
End Sub
These values are changing
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 2. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 2. FOR-NEXT LOOPS
When i = 1 row is 1
value is 6
When i = 1 row is i
value is i + 5
i = 1 i = 2 i = 3 i = 4 i = 5
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 2. FOR-NEXT LOOPS
When i = 2 row is 2
value is 7
When i = 2 row is i
value is i + 5
i = 1 i = 2 i = 3 i = 4 i = 5
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 2. FOR-NEXT LOOPS
When i = 3 row is 3
value is 8
When i = 3 row is i
value is i + 5
i = 1 i = 2 i = 3 i = 4 i = 5
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 2. FOR-NEXT LOOPS
When i = 4 row is 4
value is 9
When i = 4 row is i
value is i + 5
i = 1 i = 2 i = 3 i = 4 i = 5
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 2. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
When i = 5 row is 5
value is 10
When i = 5 row is i
value is i + 5
Let i be a counter We have 5 lines of code, so i will count from 1 to 5
Use the counter to change the row & output value
EXAMPLE 2. FOR-NEXT LOOPS
i = 1 i = 2 i = 3 i = 4 i = 5
For every line of code the row is i
and the value is i + 5
Use Lecture 4 Student Example.xlsm
Write a For-Loop for this: Remember to declare your counter! Count your loop from 1 to 5 OR from 0 to 4 Write it out the long way first if you need to
EXERCISE 2. WRITE A FOR LOOP
Write this output to these cells
(‘E’ is column 5)
Run your code to ensure it works
Use Lecture 4 Student Example.xlsm
Write a For-Loop for this: Remember to declare your counter! Count from 1 to 3 OR 0 to 2 Write it out the long way first if you need to
EXERCISE 3. WRITE A FOR LOOP
Write this output to these cells
Run your code to ensure it works
CONCATENATION &
Join together 2 or more… String variables (i.e., text or words) Text and variable values (integer, double or string) Text and constant values Variable values and constant values Etc…
Use the & to join two or more things together
CONCATENATE MEA N S TO JOIN TOGETHER
EXAMPLES OF CONCATENATION
Not 6!
EXAMPLE 3. CONCATENATION
Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer)
End Sub
Declare a variable called answer
EXAMPLE 3. CONCATENATION
Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer)
End Sub
Ask the user to enter a number Assign answer the number they enter
EXAMPLE 3. CONCATENATION
Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer)
End Sub
Ask the user to enter a number Assign answer the number they enter
EXAMPLE 3. CONCATENATION
Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer)
End Sub
Use a message box to tell the user what number they entered
EXAMPLE 3. CONCATENATION
Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer)
End Sub
Use a message box to tell the user what number they entered
EXAMPLE 3. CONCATENATION
Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer)
End Sub
The text in “ ”
EXAMPLE 3. CONCATENATION
Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer)
End Sub
Use & to join text to a variable value
EXAMPLE 3. CONCATENATION
Public Sub ConcatenateExample3() Dim answer As Double answer = InputBox(“Enter any number”) MsgBox (“You entered ” & answer)
End Sub
The number entered by the user Variables DO NOT go in “ ”
Find the procedure for Exercise 4. Using 4 input boxes, obtain the following user info: Their name The name of the Uni they attend The course they are studying Their year at Uni (i.e. 1, 2, 3, …)
Display their info in 1 MsgBox using full sentences. e.g. “Your name is…”, “You attend…University”, etc…
Create a button in a worksheet to run this code.
EXERCISE 4. CONCATENATION
Tip: to display part of a message on a new line of a Msgbox, use & vbNewLine & where you want one line to
end and another to begin.
LOOPS WITH CONCATENATION
Enter stock names in row 1 across columns 1-4
EXAMPLE 4. LOOPS WITH CONCATENTATION
Public Sub ForLoopExample4() Cells(1, 1).Value = “Stock1” Cells(1, 2).Value = “Stock2” Cells(1, 3).Value = “Stock3” Cells(1, 4).Value = “Stock4” End Sub
These values are changing
Let i be a counter We have 4 lines of code, so i will count from 1 to 4
Use the counter to change the col & output value
EXAMPLE 4. LOOPS WITH CONCATENTATION
i = 1 i = 2 i = 3 i = 4
Let i be a counter We have 4 lines of code, so i will count from 1 to 4
Use the counter to change the col & output value
EXAMPLE 4. LOOPS WITH CONCATENTATION
i = 1 i = 2 i = 3 i = 4
When i = 1 column is 1
stock is 1
When i = 1 column is i
stock is i
Let i be a counter We have 4 lines of code, so i will count from 1 to 4
Use the counter to change the col & output value
EXAMPLE 4. LOOPS WITH CONCATENTATION
i = 1 i = 2 i = 3 i = 4
When i = 2 column is 2
stock is 2
When i = 2 column is i
stock is i
Let i be a counter We have 4 lines of code, so i will count from 1 to 4
Use the counter to change the col & output value
EXAMPLE 4. LOOPS WITH CONCATENTATION
i = 1 i = 2 i = 3 i = 4
When i = 3 column is 3
stock is 3
When i = 3 column is i
stock is i
Let i be a counter We have 4 lines of code, so i will count from 1 to 4
Use the counter to change the col & output value
EXAMPLE 4. LOOPS WITH CONCATENTATION
i = 1 i = 2 i = 3 i = 4
When i = 4 column is 4
stock is 4
When i = 4 column is i
stock is i
Let i be a counter We have 4 lines of code, so i will count from 1 to 4
Use the counter to change the col & output value
EXAMPLE 4. LOOPS WITH CONCATENTATION
For every line of code the column is i
and the stock is i
Using Lecture 4 Student Example.xlsm
Write a For-Loop for this: Remember to declare your counter! Count from 1 to 3 OR 0 to 2 Write it out the long way first if you need to
EXERCISE 5. WRITE A FOR LOOP
FINAL COMMENTS
When in doubt, write it out!! If you aren’t sure how to write a loop, write it out without
using loops so you can spot the pattern. Then use a loop to make it more efficient.
Step through your code to see what is happening during each loop. Check variable values Check output to cells/ranges
You cannot loop variable names E.g., “mean” & i You have to use arrays (in a couple of weeks) For now, those bits of code will have to be inefficient and long
WHEN YOU’RE STUCK…
You are ready to move on when… LO17: You can name the 4 main parts of a For-Next loop. LO18: Given the output you need, you can write a For-Next
loop to achieve that output. Similarly, given a For-Next loop you can work out what the output/result will be (we will practice this in the lab). This is for one loop, not a nested loop (yet). LO19: You can define the concept of concatenation and
give some examples of how it is used in programming. You can use concatenation to join together values from text, variables and/or numbers. You also know when you need to use “ ” and when you should not use “ ”. LO20: You can combine the use of concatenation with for-
next loops to make code more efficient.
LEARNING OUTCOMES
THE END