# 1# 1 cs 105 spring 2010 macros: sub procedures you record what is a macro? what is with…end...

26
# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

Upload: evangeline-holmes

Post on 03-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 1CS 105 Spring 2010

Macros:Sub Procedures You Record

Macros:Sub Procedures You Record

What is a macro?

What is With…End With?

What is Sub…End Sub?

Relative vs. Absolute

Page 2: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 2CS 105 Spring 2010

Macro Statements

• A Macro always begins a Sub statement

• A Macro ends with an End Sub statement

Page 3: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 3CS 105 Spring 2010

A Macro

• To perform multiple actions on the same object Macros use

– With – End With

Page 4: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 4CS 105 Spring 2010

Select and Do—the pattern

• Note how first the Macro selects the object, then executes the code…

Page 5: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 5CS 105 Spring 2010

More about Macros…

• Step Into command helps you debug a macro

• Opening a file with a macro will prompt a question about viruses

Page 6: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 6

Recording a Macro in Office 2007

• Go to Developer, click on Record Macro

CS 105 Spring 2010

Page 7: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 7CS 105 Spring 2010

Record Macro

• Comments begin with an apostrophe (‘)

• Comments help the programmer, others to read code

Page 8: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 8CS 105 Spring 2010

Calling Macros

• You can use macros easily becausethey are on a Module sheet, that makes them available to everything, so

all you do is write the macro name

Page 9: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 9CS 105 Spring 2010

Finding the Stop Recording button in Office 2003

• We lost our little macro recorder button

• If you lose yours, go to View/Toolbars/Stop Recording, and it will appear on your spreadsheet or on a toolbar.

Page 10: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 10

Stop Recording in Office 2007

• Developer, then click on Stop Recording

CS 105 Spring 2010

Page 11: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 11CS 105 Spring 2010

Cell References in Macros

Absolute references—

When we specify exactly what cells are affected

Absolute cell address are constant

Page 12: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 12CS 105 Spring 2010

An absolute Macro

• Range selects the starting or active cell/cells—the references below are absolute—whenever the Macro runs, these exact cells are selected

First, the cell is

selected, then

a number is placed

in it

Page 13: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 13CS 105 Spring 2010

ActiveCell.FormulaR1C1

R1C1 is a reference style (you can set this under Tools/Options/General)

Don’t worry,we won’t be using thisin class. We want you to know where it came from.

Page 14: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 14CS 105 Spring 2010

ActiveCell.FormulaR1C1 = "Income"

• All that you need to know is that for the active cell, the content is "Income"

• "FormulaR1C1" refers to the contents of the active cell

• You find this notation in Macros that you create

Page 15: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 15CS 105 Spring 2010

FormulaR1C1 vs. Value

Range("D5").Select ActiveCell.FormulaR1C1 = "8" Range("D5").Select ActiveCell.Value = "8" These two code fragments produce the same results.

Page 16: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 16CS 105 Spring 2010

What if you want to vary the cells?

Page 17: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 17CS 105 Spring 2010

Relative references

We don’t always want the event to happen in the same place

• Cell addresses change

• Visual Basic uses Offset to indicate space away from active cell

Page 18: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 18CS 105 Spring 2010

Recording a Macro with Relative References

Before you start recording your Macro, click on the

Relative Reference button

Page 19: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 19

In Office 2007

• First, click on Use Relative References, then click Record Macro

CS 105 Spring 2010

Page 20: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 20CS 105 Spring 2010

Relative Cell References

– ActiveCell.Offset(2, 0).Range("A1").Select– Means the cell two rows below the active

cell, in the same column

Page 21: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 21CS 105 Spring 2010

ActiveCell.Offset(2, 0).Range("A1").Select

• What does ActiveCell.Offset(2, 0).Range("A1").Select mean?

How to I make the cell next to the active cell become the new active cell?

Page 22: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 22CS 105 Spring 2010

Range("A1").Select

• This part of the code tells you

how many cells have been selected.

ActiveCell.Offset(2, 0).Range("A1").Select means 1 cell is selected

ActiveCell.Offset(2, 0).Range("A1:C1").Select

means 3 cells in a row have been selected (if you are in cell B2, then B2, C2, and D2 are selected)

Page 23: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 23CS 105 Spring 2010

Activate or Select?

Range("B3").SelectSelection.Interior.ColorIndex = 6

Range("B3").ActivateSelection.Interior.ColorIndex = 6

• Both code fragments have the same result

Page 24: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 24CS 105 Spring 2010

This Relative Macro Causes a Crash

Page 25: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 25CS 105 Spring 2010

Run it line-by-line to find bug

Code runs OK until it reaches the line under the highlighted

line. You can turn your attention to the problem line

of code!

Page 26: # 1# 1 CS 105 Spring 2010 Macros: Sub Procedures You Record What is a macro? What is With…End With? What is Sub…End Sub? Relative vs. Absolute

# 26CS 105 Spring 2010

To Summarize

• What is a macro?

• What is With…End With?

• What is Sub…End Sub?

• Relative vs. Absolute