Top Banner
More loops 1D Arrays LECTURE 5
53
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MA3696 Lecture 5

More loops

1D Arrays LECTURE 5

Page 2: MA3696 Lecture 5

Looping userform controls

Generalising loops

Nested For-Next loops

1D Arrays

SUMMARY

Page 3: MA3696 Lecture 5

Loop Controls MORE FOR-NEXT LOOPS

Page 4: MA3696 Lecture 5

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

Page 5: MA3696 Lecture 5

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

Page 6: MA3696 Lecture 5

LOOPING USERFORM CONTROLS

The (Name) of the userform control

Page 7: MA3696 Lecture 5

LOOPING USERFORM CONTROLS

Use concatenation

Page 8: MA3696 Lecture 5

LOOPING USERFORM CONTROLS

Use the ith label

Page 9: MA3696 Lecture 5

LOOPING USERFORM CONTROLS

The column is i + 1

Page 10: MA3696 Lecture 5

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

Page 11: MA3696 Lecture 5

NESTED FOR-NEXT LOOPS

Page 12: MA3696 Lecture 5

NESTED FOR-NEXT LOOP

?

Repeat this twice

For i = 1 to 3Cells(i, 1).Value = i

Next i

Page 13: MA3696 Lecture 5

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

Page 14: MA3696 Lecture 5

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

Page 15: MA3696 Lecture 5

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

Page 16: MA3696 Lecture 5

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

Page 17: MA3696 Lecture 5

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

Page 18: MA3696 Lecture 5

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

Page 19: MA3696 Lecture 5

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

Page 20: MA3696 Lecture 5

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

Page 21: MA3696 Lecture 5

Using variables

or constants instead of

explicit numbers

GENERALISING CODE

Page 22: MA3696 Lecture 5

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

Page 23: MA3696 Lecture 5

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

Page 24: MA3696 Lecture 5

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?

Page 25: MA3696 Lecture 5

1D ARRAYS

Page 26: MA3696 Lecture 5

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)

Page 27: MA3696 Lecture 5

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

Page 28: MA3696 Lecture 5

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

Page 29: MA3696 Lecture 5

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

Page 30: MA3696 Lecture 5

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

Page 31: MA3696 Lecture 5

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

Page 32: MA3696 Lecture 5

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

Page 33: MA3696 Lecture 5

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

Page 34: MA3696 Lecture 5

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

Page 35: MA3696 Lecture 5

We had to declare multiple variables like this:

And assign values like this:

BEFORE ARRAYS…

Page 36: MA3696 Lecture 5

We can declare an array like this:

And assign values like this:

WITH ARRAYS…

Page 37: MA3696 Lecture 5

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

Page 38: MA3696 Lecture 5

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

Page 39: MA3696 Lecture 5

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

Page 40: MA3696 Lecture 5

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**

Page 41: MA3696 Lecture 5

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)

Page 42: MA3696 Lecture 5

You can ‘output’ the value of arrays to…

Cells

Ranges

Userform controls

Other variables or arrays

OUTPUTTING ARRAYS IN GENERAL

Page 43: MA3696 Lecture 5

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

Page 44: MA3696 Lecture 5

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

Page 45: MA3696 Lecture 5

WRITE SUMS USING LOOPS & ARRAYS

sum = stockPrice(1) + stockPrice(2) + stockPrice(3)

Page 46: MA3696 Lecture 5

WRITE SUMS USING LOOPS & ARRAYS

sum = stockPrice(1) + stockPrice(2) + stockPrice(3)

sum = stockPrice(1) i = 1

i = 1

Page 47: MA3696 Lecture 5

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)

Page 48: MA3696 Lecture 5

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)

Page 49: MA3696 Lecture 5

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)

Page 50: MA3696 Lecture 5

WRITE SUMS USING LOOPS & ARRAYS

Make sure the sum starts at 0

sum = stockPrice(1) + stockPrice(2) + stockPrice(3)

Page 51: MA3696 Lecture 5

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

Page 52: MA3696 Lecture 5

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

Page 53: MA3696 Lecture 5

THE END