Introduction to Excel and Visual Basic for Excel Gilbert Ritschard - - PowerPoint PPT Presentation

introduction to excel and visual basic for excel
SMART_READER_LITE
LIVE PREVIEW

Introduction to Excel and Visual Basic for Excel Gilbert Ritschard - - PowerPoint PPT Presentation

Excel and Visual Basic for Excel Introduction to Excel and Visual Basic for Excel Gilbert Ritschard Department of economics, University of Geneva http://mephisto.unige.ch Master in International Trading, Commodity Finance and Shipping


slide-1
SLIDE 1

Excel and Visual Basic for Excel

Introduction to Excel and Visual Basic for Excel

Gilbert Ritschard

Department of economics, University of Geneva http://mephisto.unige.ch

Master in International Trading, Commodity Finance and Shipping

18/9/2013gr 1/26

slide-2
SLIDE 2

Excel and Visual Basic for Excel

Outline

1

Excel, what is it?

2

Excel: Basics

3

Macro and Visual Basic (VBA) programming

18/9/2013gr 2/26

slide-3
SLIDE 3

Excel and Visual Basic for Excel Excel, what is it?

Outline

1

Excel, what is it?

2

Excel: Basics

3

Macro and Visual Basic (VBA) programming

18/9/2013gr 3/26

slide-4
SLIDE 4

Excel and Visual Basic for Excel Excel, what is it?

Excel, what is it?

Excel is a spreadsheet Interactive table (rows and columns) for managing and exploring data. Allows Computation (arithmetic and other mathematical and statistical functions) Simple data management

(sort and filter according to one or several keys alphabetic order, dates, values, ...)

Organizing and presenting tables (frame, totals, rows, columns) Graphical rendering of numerical data

(but most often requires data preprocessing)

18/9/2013gr 4/26

slide-5
SLIDE 5

Excel and Visual Basic for Excel Excel, what is it?

Excel, what is it?

Excel is a spreadsheet Interactive table (rows and columns) for managing and exploring data. Allows Computation (arithmetic and other mathematical and statistical functions) Simple data management

(sort and filter according to one or several keys alphabetic order, dates, values, ...)

Organizing and presenting tables (frame, totals, rows, columns) Graphical rendering of numerical data

(but most often requires data preprocessing)

18/9/2013gr 4/26

slide-6
SLIDE 6

Excel and Visual Basic for Excel Excel: Basics

Outline

1

Excel, what is it?

2

Excel: Basics

3

Macro and Visual Basic (VBA) programming

18/9/2013gr 5/26

slide-7
SLIDE 7

Excel and Visual Basic for Excel Excel: Basics

Spreadsheet

Rows, indexed by numbers (1,2,3,...) Columns, indexed by letters (A,B,C,...) Cell, intersection of a column (ex B) and a row (ex 3) Reference Address

  • f a cell: Column letter and row number (ex B3),
  • f a table (range): B2:D5

18/9/2013gr 6/26

slide-8
SLIDE 8

Excel and Visual Basic for Excel Excel: Basics

Spreadsheet

Rows, indexed by numbers (1,2,3,...) Columns, indexed by letters (A,B,C,...) Cell, intersection of a column (ex B) and a row (ex 3) Reference Address

  • f a cell: Column letter and row number (ex B3),
  • f a table (range): B2:D5

18/9/2013gr 6/26

slide-9
SLIDE 9

Excel and Visual Basic for Excel Excel: Basics

Spreadsheet

Rows, indexed by numbers (1,2,3,...) Columns, indexed by letters (A,B,C,...) Cell, intersection of a column (ex B) and a row (ex 3) Reference Address

  • f a cell: Column letter and row number (ex B3),
  • f a table (range): B2:D5

18/9/2013gr 6/26

slide-10
SLIDE 10

Excel and Visual Basic for Excel Excel: Basics

Inputting data and Cell content

Activate a cell by clicking on it All what you type in (text, number or formula) goes in the active cell. Validate an entry either with [Enter] or by pressing a displacement key:

←, ↑, ↓, → [Enter] (move to next row, same column) [Tab] (move to right cell) [PgUp], [PgDn]

18/9/2013gr 7/26

slide-11
SLIDE 11

Excel and Visual Basic for Excel Excel: Basics

Formula

Formula First inserted symbol is = ⇔ Formula Cell displays the result of the formula. Example:

B1 contains 3 B2 contains 4 B3 contains =B1+B2

La cellule B3 affiche 7, i.e., the sum of the content of cellesB1 et B2. We can use:

Arithmetic operators: +,−,∗,/,ˆ Functions (menu tab: Formulas): sum, average, count, max, min, ...

18/9/2013gr 8/26

slide-12
SLIDE 12

Excel and Visual Basic for Excel Excel: Basics

Formula

Formula First inserted symbol is = ⇔ Formula Cell displays the result of the formula. Example:

B1 contains 3 B2 contains 4 B3 contains =B1+B2

La cellule B3 affiche 7, i.e., the sum of the content of cellesB1 et B2. We can use:

Arithmetic operators: +,−,∗,/,ˆ Functions (menu tab: Formulas): sum, average, count, max, min, ...

18/9/2013gr 8/26

slide-13
SLIDE 13

Excel and Visual Basic for Excel Excel: Basics

Formula

Formula First inserted symbol is = ⇔ Formula Cell displays the result of the formula. Example:

B1 contains 3 B2 contains 4 B3 contains =B1+B2

La cellule B3 affiche 7, i.e., the sum of the content of cellesB1 et B2. We can use:

Arithmetic operators: +,−,∗,/,ˆ Functions (menu tab: Formulas): sum, average, count, max, min, ...

18/9/2013gr 8/26

slide-14
SLIDE 14

Excel and Visual Basic for Excel Excel: Basics

Moving and copying cells

Selecting cells

menu: Cut or Copy, on Home tab keyboard: Ctrl-X or Ctrl-C

Select a destination area either of same size or top left cell of the area. Paste

menu: Paste on left of Home tab keyboard: Ctrl-V

Mouse: To move: place mouse cursor on the border of the selected area (cursor cursor becomes an arrow), click, drag and drop by releasing the mouse button. To paste: press [Ctrl] while dragging.

18/9/2013gr 9/26

slide-15
SLIDE 15

Excel and Visual Basic for Excel Excel: Basics

Moving and copying cells

Selecting cells

menu: Cut or Copy, on Home tab keyboard: Ctrl-X or Ctrl-C

Select a destination area either of same size or top left cell of the area. Paste

menu: Paste on left of Home tab keyboard: Ctrl-V

Mouse: To move: place mouse cursor on the border of the selected area (cursor cursor becomes an arrow), click, drag and drop by releasing the mouse button. To paste: press [Ctrl] while dragging.

18/9/2013gr 9/26

slide-16
SLIDE 16

Excel and Visual Basic for Excel Excel: Basics

Moving and copying cells

Selecting cells

menu: Cut or Copy, on Home tab keyboard: Ctrl-X or Ctrl-C

Select a destination area either of same size or top left cell of the area. Paste

menu: Paste on left of Home tab keyboard: Ctrl-V

Mouse: To move: place mouse cursor on the border of the selected area (cursor cursor becomes an arrow), click, drag and drop by releasing the mouse button. To paste: press [Ctrl] while dragging.

18/9/2013gr 9/26

slide-17
SLIDE 17

Excel and Visual Basic for Excel Excel: Basics

Fill an area

Example: Conversion table between Celsius and Fahrenheit. Enter:

B1 = C,C1 = F (titres des colonnes) B2 = 0,C2 = ‘=32+(9/5)*B2’ B3 = 5

Select C2, put cursor on small square bottom-right of selected area (cursor changes to +) drag one case below. Check that C3 contains =32+(9/5)*B3 Remark: formula was copied with relative reference.

18/9/2013gr 10/26

slide-18
SLIDE 18

Excel and Visual Basic for Excel Excel: Basics

Fill an area

Example: Conversion table between Celsius and Fahrenheit. Enter:

B1 = C,C1 = F (titres des colonnes) B2 = 0,C2 = ‘=32+(9/5)*B2’ B3 = 5

Select C2, put cursor on small square bottom-right of selected area (cursor changes to +) drag one case below. Check that C3 contains =32+(9/5)*B3 Remark: formula was copied with relative reference.

18/9/2013gr 10/26

slide-19
SLIDE 19

Excel and Visual Basic for Excel Excel: Basics

Fill an area (2)

Select now area B2:C3, put cursor on the small square at bottom right of selection and drag until row 12. 1st column : sequence of numbers with increment of 5 2nd column : copy of formula with relative reference.

18/9/2013gr 11/26

slide-20
SLIDE 20

Excel and Visual Basic for Excel Excel: Basics

Relative versus absolute refrences (1)

Relative reference Formula in C2 (=32+(9/5)* B2 ) contains a relative reference to B2 (cells on left of C2). In any copy of C2, B2 will be replaced by the address of the cell on its left. Absolute reference Reference to a fixed column: Specified with a $ in front of the letter ($B2) Reference to a fixed row: Specified with a $ in front of the number (B$2) Reference to a : Specified with a $ in front of each the letter and the number ($B$2)

18/9/2013gr 12/26

slide-21
SLIDE 21

Excel and Visual Basic for Excel Excel: Basics

Relative versus absolute refrences (1)

Relative reference Formula in C2 (=32+(9/5)* B2 ) contains a relative reference to B2 (cells on left of C2). In any copy of C2, B2 will be replaced by the address of the cell on its left. Absolute reference Reference to a fixed column: Specified with a $ in front of the letter ($B2) Reference to a fixed row: Specified with a $ in front of the number (B$2) Reference to a : Specified with a $ in front of each the letter and the number ($B$2)

18/9/2013gr 12/26

slide-22
SLIDE 22

Excel and Visual Basic for Excel Excel: Basics

Relative versus absolute refrences (2)

Absolute references do not change when moved or copied Example:

A1 = quantity, B1 = price A2 = 1 B2 = 20 C2 = (unit price) A3 = 2 B3 = =A3*$B$2 A4 = 3 A5 = 5 A6 = 10

Fill the second column with content of cell B3. Check that B6, for example, contains =A6*$B$2

18/9/2013gr 13/26

slide-23
SLIDE 23

Excel and Visual Basic for Excel Excel: Basics

Some useful functions

SUM(x) sum AVERAGE(x) mean value VAR.P(x) variance VAR.S(x) estimated variance STDEV.P(x) standard deviation STDEV.S(x) estimated standard deviation MEDIAN(x) median QUARTILE(x, k) kth quartile COVARIANCE.P(x, y) covariance CORREL(x, y) Pearson linear correlation TRANSPOSE(A) Transpose of matrix A MMULT(A, B) Product of matrices AB MDETERM(A Determinant of A MINVERSE(A) Inverse of matrix A LN(x) natural logarithm LOG(x, b) logarithm to base b EXP(x) e raised to the power x: exp(x) = ex

18/9/2013gr 14/26

slide-24
SLIDE 24

Excel and Visual Basic for Excel Excel: Basics

Probability distributions

(cumulated when c = TRUE) NORM.DIST(x, µ, σ, c) normal distribution N(µ, σ2) NORM.S.DIST(x, c) standardized normal distribution N(0, 1) CHISQ.DIST(x, d) Chi-square distribution for d degrees of freedom NORM.INV(p, µ, σ) inverse of normal distribution NORM.S.INV(p) inverse of N(0, 1) distribution CHISQ.INV(p, d) inverse of Chi-square distribution

18/9/2013gr 15/26

slide-25
SLIDE 25

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Outline

1

Excel, what is it?

2

Excel: Basics

3

Macro and Visual Basic (VBA) programming

18/9/2013gr 16/26

slide-26
SLIDE 26

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Recording a macro

Best way to start with VBA programming, is

1

record a macro

2

look at the macro

3

edit the macro

18/9/2013gr 17/26

slide-27
SLIDE 27

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-28
SLIDE 28

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-29
SLIDE 29

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-30
SLIDE 30

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-31
SLIDE 31

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-32
SLIDE 32

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-33
SLIDE 33

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-34
SLIDE 34

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-35
SLIDE 35

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-36
SLIDE 36

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Example

Fill a range with values from 1 to 20 and put a heading ‘x‘. Next to column x, create a column y with 20 random numbers

(by inserting =RAND() in the cells)

Rename the Sheet as ‘My Data’. Now we want to create a macro that

1

makes a copy of the values (not the formula) in the range of the x and y variables in a new sheet

2

renames the new sheet as ‘Outcome‘

Record a first macro

1

Go to the ‘Developer’ menu tab, and click on ‘Record Macro’

(you will be asked to give a name for the macro: name it ‘MyFirstMacro‘)

2

Make the wanted operations

3

Click on ‘Stop Recording’.

18/9/2013gr 18/26

slide-37
SLIDE 37

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Running and editing the macro

To run the macro, click on ‘Macros’ and select the macro you want to launch. As recorded, MyFirstMacro will end on an error. Clicking ‘debug’, will open the recorded VBA script and highlight the faulty line. Problem is that the newly created sheet has a different name than the one created when recording the macro (e.g., Sheet5 instead of Sheet4) Edit the macro to replace the faulty line with

Sheets.Add ActiveSheet.Name = "Outcome"

and simply delete any other line referring to ‘Sheet4‘.

18/9/2013gr 19/26

slide-38
SLIDE 38

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Deleting an existing sheet

The macro works, but we need to first delete the existing ‘Outcome’ sheet when it exists. We automatize that with the following code

CurrentSheet = Format(ActiveSheet.Name) For Each nm In ActiveWorkbook.Sheets If nm.Name Like "Outcome" Then If nm.Delete Then Exit For End If Next nm Sheets(CurrentSheet).Select

18/9/2013gr 20/26

slide-39
SLIDE 39

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Creating a button for launching the macro

Go on ‘My Data’ sheet. On the Developer tab, Open the ‘Insert’ list and select the top left ‘Button (Form Control)’ A dialog will ask you to select a macro: select ‘MyFirstMacro’ and click OK Click on ‘Design Mode’, then with the right mouse button on the created button and select ‘Edit text’ on the menu which pops up. Replace the text with the macro name or whatever you want. Click again on ‘Design Mode’ when you are done. Now, just try it!

18/9/2013gr 21/26

slide-40
SLIDE 40

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Looping though cells using Cells(i,j)

To loop through cells use Cells(i,j) To illustrate, write a macro to collect in z and w columns respectively the x and y values of cases for which y < .5 For the loop, use for i = 3 to 22

18/9/2013gr 22/26

slide-41
SLIDE 41

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Solution

Sub MySelectMacro() ’ ’ MySelectMacro Macro ’ Dim i As Integer Dim ii As Integer Cells(2, 5) = "z" Cells(2, 6) = "w" ii = 3 For i = 3 To 22 If Cells(i, 3) < 0.5 Then Cells(ii, 5) = Cells(i, 2) Cells(ii, 6) = Cells(i, 3) ii = ii + 1 End If Next i ’ End Sub

18/9/2013gr 23/26

slide-42
SLIDE 42

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Cells properties

VBA is an object oriented language, with objects, properties and methods

This means that when applied to an object, the behavior of the method is determined according to the object properties.

This is for instance of importance when assigning values to Cells or Ranges, where it may be of importance to distinguish between properties

Cells.Value Value in the cell Cells.Formula Formula in the cell with standard referencing Cells.FormulaR1C1 Formula in the cell with R1C1 referencing

R1C1 references can be turned in ‘Options’ (File tab), under ‘Formulas’.

18/9/2013gr 24/26

slide-43
SLIDE 43

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Useful links

Getting Started with VBA in Excel 2010 http://msdn.microsoft.com/en-us/library/ ee814737(v=office.14).aspx Creating VBA Macros to Manipulate Worksheets in Excel 2007 http://msdn.microsoft.com/en-us/library/ dd553655(v=office.12).aspx Excel VBA Easy (100 examples) http://www.excel-vba-easy.com/

18/9/2013gr 25/26

slide-44
SLIDE 44

Excel and Visual Basic for Excel Macro and Visual Basic (VBA) programming

Bibliography I

Albright, S. C., W. Winston, and C. Zappe (2008). Data Analysis and Decision Making with Microsoft Excel (3rd Edition) (3rd ed.). South-Western College Publishing. Levine, D. M., D. F. Stephan, T. C. Krehbiel, and M. L. Berenson (2008). Statistics for Managers: Using Microsoft Excel (5th ed.). Upper Saddle River, NJ, USA: Prentice Hall. Wells, E. and S. Harshbarger (1997). Microsoft Excel 97 Developer’s

  • Handbook. Redmond WA: Microsoft Press.

18/9/2013gr 26/26