ma3696 lecture 9

50
Range objects User-Defined Functions LECTURE 9

Upload: brunel-university

Post on 28-Jun-2015

100 views

Category:

Education


0 download

TRANSCRIPT

Page 1: MA3696 Lecture 9

Range objects

User-Defined Functions

LECTURE 9

Page 2: MA3696 Lecture 9

Range objects

Obtaining a range using the RefEdit control

User-Defined Functions

Without arguments

With arguments Variables, constants, arrays and range objects

SUMMARY

Page 3: MA3696 Lecture 9

A cross between a named range and an array

RANGE OBJECTS

Page 4: MA3696 Lecture 9

Range objects are an alternative to using arrays and named ranges.

Range objects are preferred when:

You are using the RefEdit control to allow users to select data for a program.

You are creating user defined functions which you want to use within Excel worksheets (as opposed to using them solely within your code).

You are creating an Excel add-in and don’t want to clog up the user’s workbook with range names.

RANGE OBJECTS

Page 5: MA3696 Lecture 9

A Range is a data type. So we declare a range object as:

Where rangeObj is the name you want to assign to the range object

Assign a range object a range within a procedure:

Where Range is a range in Excel and RangeName is a named range in Excel.

For example,

DECLARING A RANGE OBJECT

Dim rangeObj as Range

Set rangeObj = Range Set rangeObj = Range(“RangeName”)

Set stockRange = Range(Cells(1,1), Cells(1,25))

Set stockRange = Range(“StockPrices”)

Page 6: MA3696 Lecture 9

You can do nearly everything the same as a range or named range in Excel. For example:

Count rows or columns

Refer to cells within the range

METHODS APPLIED TO RANGE OJBECTS

rangeObj.Rows.Count rangeObj.Columns.Count

rangeObj.Cells(i, j).Value

Page 7: MA3696 Lecture 9

Letting the user select the range of data

REFEDIT CONTROL

Page 8: MA3696 Lecture 9

RefEdit is a UserForm control:

Allows user to select a range in Excel

Set this selection as a range object in your code

Where RefEdit is the (Name) of the control

Then use the range however you need to

In a built-in function

In a user defined function

In place of a named range

REFEDIT

Set rangeObj = Range(RefEdit.Value)

Page 9: MA3696 Lecture 9

Open Lecture 9 Student Example.xlsm

Open UserForm2

In the sub for the command button code the following:

Declare a range object called userSelect

Set this range object equal to the value of the RefEdit control

Test that the works by outputting the range object to some cells Use the .Columns.Count and .Rows.Count property to assist you

Start the output in Cells(1,6) of Sheet 1

Remember, to ‘copy’ or ‘output’ you just need to set the two ranges equal to each other

(make sure to put .Value at the end of each range)

EXERCISE 1. REFEDIT

Page 10: MA3696 Lecture 9

Create a function in VBA

USER DEFINED FUNCTIONS

Page 11: MA3696 Lecture 9

Designed/created by YOU, the USER

Similar to an Excel built-in function

Returns a value: number, text, True/False

Always goes into a Module (not a UserForm module)

Private Function – Only used in the module it’s in

Public Function – Used in any module or in Excel

Can be executed in two ways:

Called from within a Sub Procedure in VBA, OR

Used from an Excel worksheet: Click on a cell, type =, then the name of your function

WHAT IS A USER DEFINED FUNCTION?

Page 12: MA3696 Lecture 9

Functions only do calculations

Functions DO NOT output to cells or userforms

Do not put commands to write to cells in your function

Do not put commands to write to userform controls in your function

A Sub procedure performs a series of actions

Call your function from a Sub and write the result to cells or userforms from your Sub

FUNCTION DOS AND DON’TS

Page 13: MA3696 Lecture 9

This is called the ARGUMENT.

Apply an operation to the argument and output the result

For SUM(), it specifies what this function should sum

In this case, we are specifying an array/range

User defined functions can have arguments too

ARGUMENT . AN EXAMPLE USING THE SUM FUNCTION.

Page 14: MA3696 Lecture 9

The function operates on an argument to return a value.

Some functions can return a value without operating on a variable.

ARGUMENT . WHEN IS IT NEEDED?

1 or more numbers 1 or more arrays/ranges

1 number

NO argument needed

Page 15: MA3696 Lecture 9

Remember Functions go in Modules!

EXAMPLE 1. MULTIPLY 2 NUMBERS

Page 16: MA3696 Lecture 9

Requires 2 variables as the argument (because I want it to multiply 2 numbers together)

I’ve named them x & y

EXAMPLE 1. MULTIPLY 2 NUMBERS

Page 17: MA3696 Lecture 9

MUST assign each argument a data type

EXAMPLE 1. MULTIPLY 2 NUMBERS

Page 18: MA3696 Lecture 9

The Function itself MUST be assigned a value WITHIN the Function

If you don’t, it will NOT return a value

EXAMPLE 1. MULTIPLY 2 NUMBERS

Page 19: MA3696 Lecture 9

Use your function like a built-in Excel function

• Type = then the name of the User-Defined function.

• Select the function from the list.

• Select/Enter the arguments. • Separate the arguments

with a comma.

USE YOUR FUNCTION IN EXCEL

Page 20: MA3696 Lecture 9

Open Lecture 9 Student Example.xlsm

Open Module1

Insert a function called Add2numbers

Give this function 2 arguments, x and y

Write code so that this function adds the value of x and y

Test your code in an Excel worksheet – does it work?

EXERCISE 2. CREATE AN ADD FUNCTION

Page 21: MA3696 Lecture 9

CALLING FUNCTIONS FROM A SUB

Page 22: MA3696 Lecture 9

CALLING FUNCTIONS FROM A SUB

Page 23: MA3696 Lecture 9

CALLING FUNCTIONS FROM A SUB

The name of the Function

Page 24: MA3696 Lecture 9

CALLING FUNCTIONS FROM A SUB

This function needs 2

arguments.

Page 25: MA3696 Lecture 9

CALLING FUNCTIONS FROM A SUB

Page 26: MA3696 Lecture 9

CALLING FUNCTIONS FROM A SUB

The name of the Function

Page 27: MA3696 Lecture 9

CALLING FUNCTIONS FROM A SUB

This function needs 2

arguments.

Page 28: MA3696 Lecture 9

Type this code and run the UserForm.

Does it work (numbers only)?

EXERCISE 3. CALLING FUNCTIONS

Page 29: MA3696 Lecture 9

FUNCTIONS USING ARRAYS

Page 30: MA3696 Lecture 9

The name of the Function

EXAMPLE 2. AVERAGE OF AN ARRAY

Page 31: MA3696 Lecture 9

EXAMPLE 2. AVERAGE OF AN ARRAY

The argument is an array

Any array we send to this function will be called array1 within

the function

Page 32: MA3696 Lecture 9

EXAMPLE 2. AVERAGE OF AN ARRAY

The number of rows in array1() is not

known in the function

Ubound(array1) returns the number of rows in array1

Page 33: MA3696 Lecture 9

EXAMPLE 2. AVERAGE OF AN ARRAY

The Function is given a value

within the Function

Page 34: MA3696 Lecture 9

We can’t use it like a built-in function because the argument is an array.

Call it from a Sub procedure

EXAMPLE 2. AVERAGE OF AN ARRAY

Page 35: MA3696 Lecture 9

CALL A FUNCTION FROM A PROCEDURE

Page 36: MA3696 Lecture 9

CALL A FUNCTION FROM A PROCEDURE

Page 37: MA3696 Lecture 9

CALL A FUNCTION FROM A PROCEDURE

Page 38: MA3696 Lecture 9

CALL A FUNCTION FROM A PROCEDURE

Page 39: MA3696 Lecture 9

Function procedures

Used to return a value

Sub procedures

Use to run a series of commands

Does NOT return a value

For example,

If you want to output the result of a calculation in a cell Do the calculation in a function

In a Sub Procedure, output that function to a cel l

FUNCTION V SUB PROCEDURE

Let the cells = the value of the function within

the Sub

Page 40: MA3696 Lecture 9

Open Module2

Insert a function called AverageArray2D

Inside this function write code to find the average of a 2D array.

You can start with the code for the 1D array and just modify it.

The code to calculate the number of columns in an array is UBound(arrayName, 2) Recall the code to calculate the number of rows in a array is

Ubound(arrayName)

To check that your code works, modify Sub MainSub() so that you read in a 2D array from Excel

(use the first 2 or 3 columns – it ’s up to you)

Output AverageArray2D from a MsgBox in Sub MainSub()

EXERCISE 4. FUNCTIONS USING ARRAYS

Page 41: MA3696 Lecture 9

FUNCTIONS USING RANGE OBJECTS

Page 42: MA3696 Lecture 9

Insert a function called SumOfRange

In this function add together all elements of the range:

Test this function in an Excel Worksheet

EXAMPLE 3. SUM A RANGE OBJECT

Range object

Count the number of rows and cols for the loops

Use .Cells() just like with a named range

Use a nested loop because you have rows

and columns of data

Page 43: MA3696 Lecture 9

Open Module2

Insert a function called AverageOfRange

Write code to calculate the average All you really need to do is modify one of your previous functions

Test your function in Excel – make sure it works

EXERCISE 5. FUNCTIONS USING RANGES

Page 44: MA3696 Lecture 9

CALLING PROCEDURES

Page 45: MA3696 Lecture 9

Very similar to calling a Function

Just write Call followed by the name of the Sub

If I had a Sub Procedure called EnableControls() and wanted to call it from within a different Sub, I’d write:

Call EnableControls

If the sub has arguments, then write them in brackets, just like for a function

CALLING A SUB FROM WITHIN ANOTHER SUB

For Example

Page 46: MA3696 Lecture 9

Use Sub procedures for

Tasks you want to repeat in your code

Enable textboxes

Call this sub from within another procedure

USING SUB PROCEDURES

For Example

Page 47: MA3696 Lecture 9

Use Sub procedures for

Tasks you want to repeat in your code

Breaking a large chunk of code into smaller bits

USING SUB PROCEDURES

For Example

Page 48: MA3696 Lecture 9

Use Sub procedures for

Tasks you want to repeat in your code

Breaking a large chunk of code into smaller bits

Using Subs in this way is ‘good practice’, but not obligatory.

USING SUB PROCEDURES

Page 49: MA3696 Lecture 9

You are ready to move on when… LO35: You can declare and assign a range to a range object.

You also understand when and why a range object would be used in place of a named range or array.

LO36: You can describe what the RefEdit tool is used for. In addition, you can assign the value of the RefEdit tool to a range object.

LO37: You can describe the difference between a Sub procedure and Function procedure. Further, you can define what arguments are in relation to functions.

LO38: You can write and read functions with and without arguments (to a reasonable standard). In addition, for functions with arguments you understand the difference between using an array, variable or range object and can apply that knowledge in practice.

LEARNING OUTCOMES

Page 50: MA3696 Lecture 9

THE END