static macros - excel unusualexcelunusual.com/wp-content/uploads/2011/02/staticmacros.pdfstatic...

19
Static Macros by George Lungu <excelunusual.com> 1

Upload: others

Post on 04-Apr-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

Static Macros

by George Lungu

<excelunusual.com>

1

Page 2: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

``

All Windows stuff seemed awkward not to say annoying at the

beginning. That means navigating through directories, opening

files, saving and renaming and overall I wasn’t too impressed

with anything Excel or the Office suite had to offer except for

the fact that I didn’t have to waste paper like with a type writer

RIT – my old school in Rochester NY

An old Windows logo

I started using Excel around 1993 while in

graduate school.

Later I employed Excel for processing data in

my thesis work but now I realize I was using

it in a very inefficient fashion. You can see all

these books and sites around promising

increased productivity…, well I was not too

productive but I got the work done anyway.2

Page 3: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

One of the first important things I learned

is that Excel will normally copy formulas in

relative reference with neighboring cells. You

can make a reference absolute with

respect to a column, a row or both a column

and a row by using the dollar sign ($).

You can find out more by

opening the “Help” menu in

Excel and typing “absolute

referencing”

If you still need more clarification

you can search Google or

YouTube.

3

Page 4: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

In 2003-2004 I was looking for trends in

the stock market using Excel. I needed

to do parametric analysis which meant

calculating a certain trading strategy

work sheet for a family of constants and

storing/plotting the results as an array.

It was then when I was introduced to

visual basic. I was shown how to create

a button in Excel and associate it with a

simple Visual Basic code called a

macro.

And there was light!

4

Page 5: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

Enabling macros in Excel

In Excel 2003 or older:

Tools => Macro => Security => Low (or Medium) => OK => Save => Close =>

Reopen Workbook

In Excel 2007:

Right Click the Office Button => Excel Options => Trust Center => Trust Center

Settings => Macro Settings => Disable all macros with notification => OK => Save

and Close => Reopen Workbook

When you reopen a file with macros if you get a “Security Warning” do the following:

Options => Enable this content => OK

In Excel 2010 – being done in two stages:

A) Open File => File => Options => Trust Center => Trust Center Settings => Macro

Settings => Enable all macros … => OK => OK => Save

B) File => Options => Trust Center => Trust Center Settings => Protected View =>

Disable them … => OK => OK => Save => Close => Reopen Workbook5

Page 6: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>Where to find and create macros:

In Excel 2003 and earlier

go to Tools => Macro =>

Visual Basic Editor

or simply type “Alt F11”

Standard Code Modules, also

called Modules, are where you put

most of your VBA code. Your basic

macros and your custom function

(User Defined Functions) should be

in these modules.

Workbook And Sheet

Modules are special

modules tied directly to the

Workbook object and to

each Sheet object.

For the novice programmer, all your code should be in standard modules. For

example a function will work only while written in a module. The modules have

a much larger scope and they could even be shared between workbooks.

6

Page 7: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

<excelunusual.com>

A Spin Button Macro in Visual Basic for Applications (VBA)

This macro will be controlled by a “spin button” and it will adjust the integer value

of a certain cell between the lower limit of 1 and the higher limit of 10.

Instructions:

- Open a new workbook

- View -> Toolbox -> Control Toolbox -> Design Mode (click)

- In the control toolbox click “Spin Button” and trace out (drag) a

button somewhere around cell “C3”

- Right click the button and select “Properties”

- In the property box replace the name “SpinButton1” with

“CountB4” (or some other name you like)

- In the property box replace “min” with 1 and “max” with 10

7

Page 8: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

<excelunusual.com>Spin button

Control toolbox

Properties box

Design mode icon

A Spin Button Macro

(continuation)

8

Page 9: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

- You can change colors and other properties to your taste

- Double click the button. The Visual Basic editor window comes up

with the following code:

- Replace the code with the following:

- Now get back to the “Control Toolbox” and exit the design mode by

clicking on the icon with the same name.

- Close the control toolbox window and try to see if the button you just

created works as expected

Private Sub CountB4_Change()

End Sub

Private Sub CountB4_Change()

Range("B4") = CountB4.Value

End Sub

A Spin Button Macro

(continuation)

9

Page 10: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>Verification:

- Let’s associate a chart with cell B4

- Select cell “B4” => Insert => Chart => Column => Finish

- Select the chart => Format Y axis => Maximum = 0, Minimum = 12

- Click the spin button and see the effects on the chart

A Spin Button Macro

(continuation)

10

Page 11: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

<excelunusual.com>

Sometimes the correspondence between the value of the cell and the value of the

macro argument is not a linear one but can be described as a look-up table. In this

case we will use the “Select Case” construct in VBA. The syntax for the Case

statement is:

A “Select-Case” Macro in Visual Basic for Applications (VBA)

Select Case test_expression

Case condition_1

result_1

……………………

……………………

Case condition_n

result_n

Case Else

result_else

End Select

test_expression is a string or numeric value. It is the value that you are

comparing to the list of conditions (labeled from 1 to n). Once a condition is

found to be true, the Case statement will execute the corresponding code and

not evaluate the conditions any further.11

Page 12: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

<excelunusual.com>The “Select-Case” Macro - continuation

Instructions:

1. In the existing work sheet (Sheet_1) enter the design mode by

doing the following:

View -> Toolbox -> Control Toolbox -> Design Mode (click)

2. In the control toolbox click “Spin Button” and trace out (drag) a

button somewhere around cell “O3”

3. Right click the button and select “Properties”

4. In the property box replace the name “SpinButton1” with

“Select_CaseN4” (or some other name you like)

5. In the property box replace “min” with 1 and “max” with 7

6. You can change colors and other properties to your taste

7. Double click the button. The Visual Basic editor window comes up

with the following code:

Private Sub CountB4_Change()

End Sub

12

Page 13: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

Instructions:

- Replace the macro

code with the following:

The “Select-Case” Macro - continuation

Private Sub C_S_N4_Change()

Select Case Case_SelectN4

Case 1

Range("N4") = 0.1

Case 2

Range("N4") = 0.2

Case 3

Range("N4") = 0.5

Case 4

Range("N4") = 1

Case 5

Range("N4") = 2

Case 6

Range("N4") = 5

Case Else

Range("N4") = 10

End Select

End Sub13

Page 14: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

Verification:

- Now get back to the “Control Toolbox” and exit the design mode by

clicking on the icon with the same name.

- Close the control toolbox window and try to see if the button you just

created works as expected

The “Select-Case” Macro - continuation

You are supposed to be able to go

through the following combination of

values in Cell “N4” in any diraction:

0.1 => 0.2 => 0.5 => 1 => 2 => 5 =>10

0.1 <= 0.2 <= 0.5 <= 1 <= 2 <= 5 <=10

14

Page 15: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

<excelunusual.com>

Often times, the output of a macro must be expressed as a function of the macro’s

argument. This function might not exist in VBA and in this case we will use the

following construct: Application.Function(), where “Function” is the name of the

equivalent spreadsheet function. This way we can use a large variety of functions

nonexistent in VBA.

Instructions:

- After creating a spin button, right click it and select

“Properties”

- In the property box replace the name “SpinButton1” with

“RotateB4” (or some other name you like)

- Double click the button and replace the existing Visual Basic

code with:

The “Application” Macro in Visual Basic for Applications (VBA)

Private Sub AppR4_Change()

Range("R4") = Application.Round(AppR4.Value / 17, 1)

Range("R5") = Application.SumIf(Range("R8:R15"), ">0")

End Sub

15

Page 16: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

This macro uses the spreadsheet function

“Round” to find the truncated value (with one

digit past the decimal point) of the macro

argument divided by 17 and display it in cell

“R4”. (search Help for the function “Round”)

The macro also takes the positive values in

the range “R8:R15” and sums them up,

displaying the result in cell “R5” (search

Help for the function “Sumif”)

The “Application” Macro (continuation)

16

Page 17: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

<excelunusual.com>

This macro will be controlled by a “spin button” and it will adjust the integer value of a

certain cell between the lower limit of 0 and the higher limit of 355 in increments of 5

degrees as an infinite loop. This macro uses two “If” statements in order to get the

angle to loop around between the values 0 and 355.

Instructions:

- View -> Toolbox -> Control Toolbox -> Design Mode (click)

- In the control toolbox click “Spin Button” and trace out (drag) a button

somewhere around cell “W3”

- Replace the name “SpinButton1” with “RotV4”

- In the property box replace “min” with -1 and “max” with 80

- Double click the button and replace the existing Visual Basic code with:

The “Rotation” Macro in Visual Basic for Applications (VBA)

Private Sub RotV4_Change()

If RotV4 > 71 Then RotV4 = 0

If RotV4 < 0 Then RotV4 = 71

Range("V4") = 5 * RotV4.Value

End Sub

17

Page 18: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>

In order to easily verify the functionality we will have to add a 2x2 table:

Cell “V9”=0

Cell “W9”=0

Cell “V10”= cos(radians(V4))

Cell “W10”= sin(radians(V4))

The “Rotation” macro (continuation)

We’ll add an XY scatter plot too:

The “X values” will be from the range “V9:V10”

The “Y values” will be from the range “W9:W10”

The axes will have a range between -1.2 to 1.2

Clicking the spin button will rotate the charted

segment in both directions.

18

Page 19: Static Macros - Excel Unusualexcelunusual.com/wp-content/uploads/2011/02/StaticMacros.pdfStatic Macros by George Lungu  1  `` All Windows

<excelunusual.com>Conclusions: Five different shape alterations were shown in this presentation:

1. Resizing – which can be done independently on each axis by multiplying the

coordinates by a certain factor while the shape is centered in the origin.

2. Translation – which can be done independently on each axis by adding or

subtracting a term to each coordinate.

3. Simple rotation – formulas were derived for rotation around the origin by an angle

a of a shape centered in origin.

4. Rotation of a shape around its own axis – the exact derivation was not given

but the recipe is to translate the shape back to origin, then rotate it and then translate

the shape to it’s final position (translation + rotation + translation)

5. Scene rotation – this is just a simple rotation but the shapes in the scene are held

fixed at their own coordinates.

A MS Excel 2003 work book with all the examples can be downloaded.

The reader is advised to examine the functionality of each section of the

work sheet and try to rebuild it from scratch reading as little as possible.

This presentation should be seen as a lure or a lifeline. The farther you can

reach with the least reading, the more knowledge you’ll gain. Your only real

teacher lives in yourself. Spend few days, don’t rush!19