More loops 1D Arrays LECTURE 5
More loops
1D Arrays LECTURE 5
Looping userform controls
Generalising loops
Nested For-Next loops
1D Arrays
SUMMARY
Loop Controls MORE FOR-NEXT LOOPS
LOOPING USERFORM CONTROLS
Public Sub Command_LoopControls_Click()
Label1.Caption = Cells(2, 2).Value Label2.Caption = Cells(2, 3).Value Label3.Caption = Cells(2, 4).Value
End Sub
These values are changing
Labels 1, 2 and 3
LOOPING USERFORM CONTROLS
Public Sub Command_LoopControls_Click()
Label1.Caption = Cells(2, 2).Value Label2.Caption = Cells(2, 3).Value Label3.Caption = Cells(2, 4).Value
End Sub
The label is i The column is i + 1
i = 1
i = 2
i = 3
LOOPING USERFORM CONTROLS
The (Name) of the userform control
LOOPING USERFORM CONTROLS
Use concatenation
LOOPING USERFORM CONTROLS
Use the ith label
LOOPING USERFORM CONTROLS
The column is i + 1
Download Lecture 5 Student Example.xlsm
Write a For-Loop for this code in UserForm1.
Remember to declare your counter!
Questions:
Are the cells being assigned the value of the textboxes?
Or are the textboxes being assigned the values of the cells?
EXERCISE 1. WRITE A FOR LOOP
Public Sub LoopExercise1_Click()
Range(“Investment”).Columns(1).Value = TextBox1.Value Range(“Investment”).Columns(2).Value = TextBox2.Value Range(“Investment”).Columns(3).Value = TextBox3.Value
End Sub
NESTED FOR-NEXT LOOPS
NESTED FOR-NEXT LOOP
?
Repeat this twice
For i = 1 to 3Cells(i, 1).Value = i
Next i
NESTED FOR-NEXT LOOP
Repeat this twice
For k = 1 to 2 Next k
For i = 1 to 3Cells(i, 1).Value = i
Next i
NESTED FOR-NEXT LOOP
Repeat this twice
For k = 1 to 2 For i = 1 to 3
Cells(i, 1).Value = i Next i
Next k
For i = 1 to 3Cells(i, 1).Value = i
Next i
NESTED FOR-NEXT LOOP
Repeat this twice
For k = 1 to 2 For i = 1 to 3
Cells(i, k).Value = i Next i
Next k
For i = 1 to 3Cells(i, 1).Value = i
Next i
Use Lecture 5 Student Example.xlsm
LoopExercise2 in Module1
Write a nested For-Loop for this output.
Remember to declare your counter.
EXERCISE 2. NESTED FOR-NEXT LOOP
Repeat this twice
EXERCISE 2. SOLUTIONS
Cells(1, 1).Value = 1 Cells(1, 2).Value = 2 Cells(1, 3).Value = 3
Cells(2, 1).Value = 1 Cells(2, 2).Value = 2 Cells(2, 3).Value = 3
For i = 1 to 3Cells(1, i).Value = i
Next i
Repeat this twice
EXERCISE 2. SOLUTIONS
Cells(1, 1).Value = 1 Cells(1, 2).Value = 2 Cells(1, 3).Value = 3
Cells(2, 1).Value = 1 Cells(2, 2).Value = 2 Cells(2, 3).Value = 3
For k = 1 to 2 For i = 1 to 3
Cells(1, i).Value = i Next i
Next k
EXERCISE 2. SOLUTIONS
Cells(1, 1).Value = 1 Cells(1, 2).Value = 2 Cells(1, 3).Value = 3
Cells(2, 1).Value = 1 Cells(2, 2).Value = 2 Cells(2, 3).Value = 3
For k = 1 to 2 For i = 1 to 3
Cells(k, i).Value = i Next i
Next k
Use Lecture 5 Student Example.xlsm
LoopExercise3 in Module1
Write a nested For-Loop for this output.
Remember to declare your counter.
EXERCISE 3. NESTED FOR-NEXT LOOPS
Repeat this three times
Using variables
or constants instead of
explicit numbers
GENERALISING CODE
WHAT DOES IT MEAN TO GENERALISE?
Dim i As Integer
For i = 1 to 10 Cells(i +1, 1).Value = i Next i
Dim i As Integer
Const TIMEPERIODS = 10
For i = 1 to TIMEPERIODS Cells(i +1, 1).Value = i Next i
I know the number of TIMEPERIODS
in advance
WHAT DOES IT MEAN TO GENERALISE?
Dim i As Integer
Dim nStocksSelected As Integer
For i = 1 to nStocksSelected […..] Next i
Unknown until the user runs the program
I only want this code to run for the number of stocks selected by the user
Replace specific numbers with variables
And assign those variables a value
Enables code to be changed/updated quickly
Only have to change one value instead of every value
Enables you to use user inputs in your loops
You don’t know these in advance
WHAT DOES IT MEAN TO GENERALISE?
1D ARRAYS
Hold a range (or set) of values
Basically a set of variables
Your arrays will mainly be 1 or 2 dimensions
arrayName is the name of your array
ARRAYS
arrayName(5) arrayName(5) arrayName(5, 4)
Hold a range (or set) of values
Basically a set of variables
Your arrays will mainly be 1 or 2 dimensions
arrayName is the name of your array
ARRAYS
arrayName(5, 4) arrayName(5) arrayName(5)
Today is only about 1D Arrays
The same rules apply to arrays as to variables
Disposable, local or global? – Declare in the right place
Must specify a data type
Also, need to specify the size of the array
The “size” is the number of elements in the array
DECLARING ARRAYS
Public Sub DescriptiveStats()
Dim stockPrices(5) As Double
Dim stockRet(4) As Double
End Sub
5 stock prices
4 stock returns
The same rules apply to arrays as to variables
Disposable, local or global? – Declare in the right place
Must specify a data type
Also, need to specify the size of the array
The “size” is the number of elements in the array
DECLARING ARRAYS
Public Sub DescriptiveStats()
Dim stockPrices(5) As Double
Dim stockRet(4) As Double
End Sub
5 stock prices
4 stock returns
5 stock prices 4 stock returns
Many times we don’t know the number of elements that will be in the array.
So, first declare them empty (disposable, local or global):
DECLARING ARRAYS
Dim stockPrices() As Double Dim stockRet() As Double
Public Sub DescriptiveStats() End Sub
arrayName() As DataType
arrayName:= the name of the array DataType:= Integer, Double, String…
General format for declaration
For example, these are local
After the size of the array is know, re-declare it inside a procedure
DECLARING ARRAYS
Dim stockPrices() As Double Dim stockRet() As Double Dim nDays As Integer
Public Sub DescriptiveStats()
nDays = Combobox1.value
ReDim stockPrices(nDays) As Double
ReDim stockRet(nDays-1) As Double
End Sub
User selects number of days from a
ComboBox on the userform
Arrays will most likely need to be local or
global
Redim both arrays using the variable
After the size of the array is know, re-declare it inside a procedure
DECLARING ARRAYS
ReDim arrayName(n) As DataType
n:= the number of elements in the array
General format for re-declaration
VBA counts elements of arrays starting with 0
For example,
This array is named StockPrices
It has 5 elements (i.e., 5 stock prices)
ARRAYS
StockPrices(0)
StockPrices(1)
StockPrices(2)
StockPrices(3)
StockPrices(4)
Count
0
1
2
3
4
To force VBA to count from 1
Use Option Base 1 at the top of each module
ARRAYS
StockPrices(1)
StockPrices(2)
StockPrices(3)
StockPrices(4)
StockPrices(5)
Count
1
2
3
4
5
We had to declare multiple variables like this:
And assign values like this:
BEFORE ARRAYS…
We can declare an array like this:
And assign values like this:
WITH ARRAYS…
FILLING ARRAYS FROM CELLS OR RANGES
i = 1 i = 2 i = 3 i = 4 i = 5
.
.
. i = 9
A 1D array and a single For-Next
loop so this is only for Stock 1
FILLING ARRAYS FROM CELLS OR RANGES
i = 1 i = 2 i = 3 i = 4 i = 5
.
.
. i = 9
Range Named “Prices”
Cells(1,1)
A 1D array and a single For-Next
loop so this is only for Stock 1
You can ‘fill’ arrays with values from…
Cells
Ranges
Userform controls
Other variables or arrays
Using equations (e.g., calculate returns)
FILLING ARRAYS IN GENERAL
OUTPUTTING ARRAYS TO CELLS OR RANGES
i = 1 i = 2 i = 3 i = 4 i = 5
**This is assuming we have assigned each element of the array weights() a value**
OUTPUTTING ARRAYS TO CELLS OR RANGES
i = 1 i = 2 i = 3 i = 4 i = 5
**This is assuming we have assigned each element of the array weights() a value**
Range Named “StockWeights” Cells(1,1)
You can ‘output’ the value of arrays to…
Cells
Ranges
Userform controls
Other variables or arrays
OUTPUTTING ARRAYS IN GENERAL
Use Lecture 5 Student Example.xlsm
LoopExercise4A in Module1
Declare an array for stock returns
Use that array in a For-Loop to rewrite this code.
Remember to declare your counter!
EXERCISE 4A. WRITE A FOR LOOP
Public Sub LoopExercise4A()
stockRet1 = Cells(4, 6).Value stockRet2 = Cells(5, 6).Value stockRet3 = Cells(6, 6).Value
End Sub
Now, instead of using cell references use range references to assign the array values:
Check your array has correct values by either:
Outputting the array values to different cells (any cells)
Or by using a message box
Or by stepping through your code and checking the value of each array element.
EXERCISE 4B. WRITE A FOR LOOP
Public Sub LoopExercise4A()
stockRet1 = Cells(4, 6).Value stockRet2 = Cells(5, 6).Value stockRet3 = Cells(6, 6).Value
End Sub
Public Sub LoopExercise4B()
stockRet1 = Range(“Returns”).Cells(1, 1) stockRet2 = Range(“Returns”).Cells(2, 1) stockRet3 = Range(“Returns”).Cells(3, 1)
End Sub
Exercise 4A Exercise 4B
WRITE SUMS USING LOOPS & ARRAYS
sum = stockPrice(1) + stockPrice(2) + stockPrice(3)
WRITE SUMS USING LOOPS & ARRAYS
sum = stockPrice(1) + stockPrice(2) + stockPrice(3)
sum = stockPrice(1) i = 1
i = 1
WRITE SUMS USING LOOPS & ARRAYS
sum = stockPrice(1) sum = sum + stockPrice(2)
i = 1
i = 2
i = 1 i = 2
sum = stockPrice(1) + stockPrice(2) + stockPrice(3)
WRITE SUMS USING LOOPS & ARRAYS
sum = stockPrice(1) sum = sum + stockPrice(2) sum = sum + stockPrice(3)
i = 1
i = 2
i = 3
i = 1 i = 2 i = 3
sum = stockPrice(1) + stockPrice(2) + stockPrice(3)
WRITE SUMS USING LOOPS & ARRAYS
sum = 0 sum = sum + stockPrice(1) sum = sum + stockPrice(2) sum = sum + stockPrice(3)
i = 1
i = 2
i = 3
i = 1 i = 2 i = 3
sum = stockPrice(1) + stockPrice(2) + stockPrice(3)
WRITE SUMS USING LOOPS & ARRAYS
Make sure the sum starts at 0
sum = stockPrice(1) + stockPrice(2) + stockPrice(3)
Use Lecture 5 Student Example.xlsm
LoopExercise4B in Module1
Find the sum of the 3 stock returns you assigned to an array in Exercise 4B using a For-Next loop.
Check your sum with a message box. If you get the following result, you are correct:
EXERCISE 5. WRITE A FOR LOOP
You are ready to move on when: LO21: You can use loops and concatenation to assign values to
controls on a userform or assign cells, ranges or 1D arrays values from userform controls.
LO22: You can describe what it means to generalise code. You can also determine when loops should be generalised and how to do so within your code.
LO23: You can define what a nested loop is. You can also construct basic nested loops to output values to cells or ranges. Lastly, given a nested loop you can determine the result.
LO24: You can describe the difference between a 1D and 2D array. You can also declare an array with the correct number of elements, in the correct location within your code and with the correct data type. In addition, you understand when to declare an array empty and how to use the ReDim statement.
LO25: You can assign values to a 1D array as well as assign the values of a 1D array to cells, a range or userform controls.
LEARNING OUTCOMES
THE END