Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 31

Writing Macros in Microsoft Excel 2002

http://www.rdg.ac.uk/ITS/info/training/notes/excel/macros/

Contents:

 Introduction
 Absolute and Relative References
o Using Absolute References
o Using Relative References
 Editing a Macro
o Repeating an Action
o Using Variables
 Setting Macro Security Levels
 Changing a Shortcut Key
 Deleting a Macro

See Document Conventions for information on the meaning of the text


formatting used below.
Introduction

A macro is a series of instructions which can be issued using a single


command. The macro can be invoked in various different ways - from the
keyboard (using a Control key combination), from a special icon on a toolbar or
through the menu system. Ideally, you need to know a programming language
(Microsoft Visual Basic) to create really useful macros; the examples below
give you an introduction to macro writing.
Absolute and Relative References

Whenever you write a macro you have the option of using absolute or relative
cell referencing. If you want to perform instructions on specific cells (eg move
to cell A14, calculate the sum of the values above and format the result to
appear as a currency in bold), then you use an absolute reference. If you want
to move to a cell a certain number of rows/columns away from your present
position (wherever that may be) then you use a relative reference. Hopefully
the examples below will make this clear. First, open up the example file and
set it up ready for use:
1. Load up Excel and [Open] the file called phoenix.xls in the D:\user folder
2. Right click on the PHOENIX tab and Insert… a Worksheet - press
<Return> for [OK]
3. Click again on the PHOENIX tab to move back to the data

Using Absolute References

In this first example you are going to write a macro that adds up the values in
column A and applies a bold format to the total.
1. From the Tools menu choose Macro followed by Record New Macro…

2. Set the Macro name: as Sum


3. Set the Shortcut key: to Ctrl+ S (ie <Shift> + s). Note that case matters -
try to avoid existing key combinations: <Ctrl s> is currently set to the
Save command
4. Store Macro in: This Workbook - the alternative is to store it in your
Personal Macro Workbook, which would make the macro available every
time you used Excel
5. Amend the Description: if you like (use this to say what the macro does &
who recorded it)
6. Press <Return> or click on [OK] to start recording

The Stop Recording toolbar appears -  DO NOT CLOSE it. The two buttons on
the toolbar are called [Stop Recording] and [Relative Reference]. Do not
click on either button at this stage. You now need to perform the commands
you want to constitute the macro:
7. Press <Ctrl Home> (or click on A1) to make sure the macro starts in cell
A1
8. Hold down <Ctrl> and press <down arrow> to move the active cell to the
end of column A
9. Release <Ctrl> then press <down arrow> once more to move to cell A52
10. Click on the [AutoSum] button then press <Return> to confirm the
formula
11. Press the <up arrow> key to move back to cell A52, then click on the
[Bold] button to embolden the result
12. Click on the [Stop Recording] button on the special toolbar to end the
macro

You should now have a macro which sums up the values in column A. Test out
the macro as follows:
13. First, press <Delete> to empty cell A52
14. Next, hold down the <Ctrl> and <Shift> keys together and type the letter
s - the sum appears in cell A52
15. Try adding a new value - open the Insert menu and choose Rows
16. In the now empty cell A52 type in the value 51
17. Run the macro again (<Ctrl Shift s>) and you'll find that the new value is
replaced by the original sum figure

This is a clear example of absolute references - the macro only works on the
cells for which it was recorded. Even though a control key combination was
used to move to the foot of the column, the next move (to cell A52 in step 9)
was recorded as a move to a fixed cell.
In this second example, absolute references are left turned on but this does
not effect the result. The object of this macro is to search for Blue-Green eggs
and highlight the cells in an appropriate colour.
1. From the Tools menu choose Macro followed by Record New Macro…
2. Set the Macro name: as Colour
3. Set the Shortcut key: to Ctrl+ C (ie <Shift> + c - <Ctrl c> is currently set
to copy)
4. Store Macro in: This Workbook and amend the Description: if you want
5. Press <Return> or click on [OK] to start recording
6. Open the Edit menu and issue a Find... command
7. In the Find what: box type Blue-Green
8. Press <Return> or click on [Find Next]
9. Press <Esc> or click on the [Close] button to close the Find window
10. Click on the list arrow attached to the [Fill Colour] button
11. Choose a suitable colour from the palette - eg Aqua
12. Click on the [Stop Recording] button on the special toolbar to end the
macro

You should now have a macro which colours Blue-Green cells a suitable
colour. Test out the macro by pressing the appropriate key combination:
13. Hold down the <Ctrl> and <Shift> keys and type the letter c - repeat this
a couple of times

The macro works from the active cell downwards. To demonstrate this:
14. Move the active cell down several rows
15. Hold down <Ctrl> and <Shift> and type the letter c

You will see later how to edit the macro to colour all the Blue-Green cells,
including those which were skipped.
Using Relative References

This next example is designed to copy every fifth row of data to the new sheet
(Sheet1). Here, relative references are used because each successive source
and target area is a known distance apart (5 rows down and 1 row down,
respectively):
1. Before you start the macro, move to Sheet1 - click on the Sheet1 tab
2. From the Tools menu choose Macro followed by Record New Macro…
3. Set the Macro name: as Every5
4. Set the Shortcut key: to Ctrl+ e
5. Store Macro in: This Workbook and amend the Description: if you want
6. Press <Return> or click on [OK] to start recording

Take great care to get the following instructions correct - if you make a mistake
it's probably best to abandon the recording and start again (you may have to
use a different shortcut key).
7. Click on the [Relative Reference] button
8. Click on the PHOENIX tab then press <Ctrl Home> to make A1 the active
cell
9. Hold down <Shift> and <Ctrl> and press the <right arrow> key to select
row 1
10. Click on the [Copy] button or choose Copy from the Edit menu
11. Click on the Sheet1 tab to move to the empty sheet
12. Press <Ctrl Home> to move to A1 then press <Return> for Paste
13. Press the <down arrow> key to move the active cell on Sheet1 to A2
14. Click on the PHOENIX tab to move back to the original data
15. Press the <down arrow> key FIVE times to move the active cell to A6 (or
just click on it)
16. Hold down <Shift> and <Ctrl> and press the <right arrow> key to select
the row
17. Click on the [Copy] button or choose Copy from the Edit menu
18. Click on the Sheet1 tab then press <Return> for Paste
19. Click on the [Stop Recording] button to end the macro

Test out the macro, if you like (clear the data in the cells on Sheet1 then press
<Ctrl e>) but all that happens is that the same subset of data is pasted into the
same cells on Sheet1. To complete the macro you now need to edit it.
Editing a Macro

Though recording macros is relatively simple, changing them is by no means


easy. When you edit a macro you are taken into the Visual Basic Editor, and
you need to understand exactly what the programming code is doing. Even if
you can’t program you can make minor changes and, if you add the lines of
programming detailed below, turn simple recorded macros into very useful
ones.
1. From the Tools menu choose Macro followed by Macros…
2. Select Every5 then click on [Edit]

You are now placed in the Editor, with the following code displayed:
Sub Every5()
'
' Every5 Macro
' Macro recorded 27/04/2004 to copy every fifth row to Sheet1
' Keyboard Shortcut: Ctrl+e
'
   Sheets("PHOENIX").Select
   Range("A1").Select
   Range(Selection, Selection.End(xlToRight)).Select
   Selection.Copy
   Sheets("Sheet1").Select
   Range("A1").Select
   ActiveSheet.Paste
   Application.CutCopyMode = False
   ActiveCell.Offset(1, 0).Range("A1").Select
   Sheets("PHOENIX").Select
   ActiveCell.Offset(5, 0).Range("A1").Select
   Range(Selection, Selection.End(xlToRight)).Select
   Selection.Copy
   Sheets("Sheet1").Select
   ActiveSheet.Paste
   Application.CutCopyMode = False
End Sub

Don't worry if you don't understand what appears on the screen. If you
compare it carefully with the original instructions then you might get some idea
of what is happening. Note the line which says
ActiveCell.Offset(1,0).Range("A1").Select. This is a relative reference - ie
move the active cell down 1 row and across 0 columns from the active cell.
Had you been using absolute references this line would have just read
Range("A2").Select - ie move to and select cell A2.

Tip: You can use <Alt F11> to open up the Editor direct from Excel (or switch
between it and Excel). Once you have finished making your changes, <Alt q>
can be used to close down the Editor.
Repeating an Action

Now try making some changes to the macro. If you wanted to select the 10th
row then you could amend the second relative reference to read
ActiveCell.Offset(10,0).Range("A1").Select. However, the original
objective of the macro was to select EVERY fifth row - the instructions which
have been recorded need to be repeated 10 times (as there are 50 rows in the
data set). To do this, you need to add extra lines to the middle and end of the
program:
1. Move the typing position to the start of the code to be repeated -
ActiveCell.Offset(1,0)…
2. Add the following line of code for k=1 to 10 then press <Return>
3. Click at the end of the last-but-one line - Application.CutCopyMode =
False
4. Press <Return> and type the following line of code: next k
These extra two lines of code create a loop that is repeated 10 times. The loop
counter, k, starts at 1 and increases by 1 each time the code is run. When it
gets to 10, the loop ends.
5. Close the Editor window by clicking on the [Close] window button (or
press <Alt q>) - the changes to the code are saved automatically
6. Select and <Delete> the values stored on Sheet1 then press <Ctrl e> to
run the macro

You should find that 10 rows (every 5th one) are copied across to Sheet1.
7. See if you can perform a similar edit on the Colour macro to colour all the
Blue-Green eggs

You could extend this macro to colour the Red-Brown eggs too. Simply
duplicate the code, replace Blue-Green with Red-Brown and set the
ColorIndex to 45.
Using Variables

The counter, k, in the example above is known as a variable in programming. It


varies between a starting value of 1 up to an ending value of 10. You can use
this variable in your module code:
1. From the Tools menu choose Macro followed by Macros…
2. Select Every5 then click on [Edit]
3. Change the line reading ActiveCell.Offset(5, 0).Range("A1").Select
to ActiveCell.Offset(k,0).Range("A1").Select - ie change 5 to k
4. Close the MS Visual Basic window by clicking on the close window button
5. Select and <Delete> the values stored on Sheet1 then press <Ctrl e> to
run the macro

You will find records 1, 3, 6, 10, 15, 21, 28, 36 and 45 are copied across - the
offset increases by 1 each time from 1 to 2 to 3 to 4 … to 9 (when k=10, an
empty record - row 56 - is copied across).
For this final example, you need to understand a little more about
programming. The instruction ActiveCell.Offset has numeric parameters -
real numbers like 1, 5 or 0 or numeric variables like k. On the other hand, the
instruction Range("A1") has a string parameter, A1 (here enclosed in
quotation marks). If you want to use the numeric variable k within the Range
instruction, you must first convert it to a string of characters.
6. From the Tools menu choose Macro followed by Macros…
7. Select Every5 then click on [Edit]
8. Click at the end of the line for k=1 to 10 and press <Return>
9. Type in an extra line reading m$ = "A" & Format(k)

The Format function converts the number stored in k into a string.


10. Change the line reading ActiveCell.Offset(1, 0).Range("A1").Select
to ActiveCell.Offset(1,0).Range(m$).Select - ie change "A1" to m$
11. Close the Editor window by clicking on the [Close] window button - or
press <Alt q>
12. Select and <Delete> the values stored on Sheet1 then press <Ctrl e> to
run the macro

This time the macro copies the same rows from phoenix as before, but pastes
them in the corresponding rows on Sheet1. This is because the paste is
carried out not 1 cell down from the active cell (in column A) but m$ (ie k) cells
down.
Obviously there's a lot more to macro writing than this simple introduction, but
at least you should have some idea of how macros work and how you can
change them.
Tip: If you ever want to alter the Description of a macro or change the Control
key shortcut then click on the [Options...] button in the Macro window (Tools
menu, Macro, Macros…).
Setting Macro Security Levels

Because macros have been used to spread viruses between computers,


Microsoft has introduced Security settings into Office XP. Currently, on the
PCs in the IT Services labs, this is set at the medium level in Excel, giving you
the opportunity to enable macros or not. The security check is carried out when
a file is opened. To see it in action:
1. [Save] the phoenix file then click on the [Close Window] button to close
it (but leave Excel running)
2. Open the File menu and select phoenix from the top of the list of recently
opened files - the following dialog box appears:
3. Click on the [Enable Macros] button to open the file with the macros
active

For some people, this message can be annoying; for others, a possible virus
attack is a nightmare. To set the security level you prefer:
4. Open the Tools menu, select Macro then Security - the following dialog
box appears:
     

5. Select the level of security required (here, keep it at Medium) then click
on [OK]

Setting security to High disables all macros completely; setting it to Low loads
up a file without any checking whatsoever. It is only advisable to choose this
setting if your normal anti-virus software is kept up-to-date.
Tip: If you have been sent a file containing macros, it's a good idea to disable
the macros when you first open the file and investigate them (using Edit).
Changing a Shortcut Key

If you want to change the shortcut key which runs a particular macro:
1. Open the Tools menu, choose Macro then Macros...
2. Select the macro whose shortcut key you wish to change
3. Click on the [Options...] button

4. Set the new Shortcut key: then press <Return> for [OK]

Deleting a Macro

To delete a macro you no longer want:


1. From the Tools menu choose Macro followed by Macros…
2. Select the macro to be deleted then click on [Delete]
3. You are asked to confirm the deletion - press <Return> or click on [Yes]

Writing Excel Macros


the simple way!

---------------Mini-tutorial---------------
This mini tutorial is for people who have read my introduction to writing
simple macros and who have at least looked at my OCLC Connexion Client
macros section. My macro-writing philosophy is always the same: write
macros that tell the software to do only what you need it to do and
nothing more.
Before you begin, decide which repetitive task you will be automating with
your Excel macro. Then, follow the steps below to create your macro.

Step 1:           next

Select Tools, Macros, Record new macro

Step 2:       prev           next

Type a clever name for your macro

Step 3:       prev           next

Complete all the steps in the repetitive task, then select

Tools, Macros, Stop recording


To study the syntax of your macro and/or to edit your macro, follow these
steps:

Step 1:       prev           next

Select tools, macros

Step 2:       prev           next

Click to select your macro name.


Step 3:       prev           next

Click Edit.

Step 4:       prev           next

Look at the macro code.


The macro shown here selects cells A1 through F1 and merges them.

Step 5:           prev           next

Make changes ... you could change the macro shown above to merge cells B5
through H5 ... or cells Z100 through Z500 ... or any other range ...

You can type changes ... OR, paste into your macro part of another macro you
copied from somewhere else ...

For example, you could create another macro which does something else to
cells A1 through A5 ...
The macro shown above resulted from recording while selecting cells A1 through A5,
then clicking on the center button on the formatting toolbar.

... then highlight, copy, ...


and paste ONLY the lines of code you need into your original merge macro ...

... to create a simple macro (without all those unnecessary lines) that merges
cells A1 through A5, then formats the merged cells so that any text you
enter in them will be centered.
And, of course, in many cases, you can DELETE LINES! (If you would like to
see how I reduced needlessly long macros generated by the Excel macro
recorder to much shorter ones, click here)

To run your macro:


Step 1:       prev           next

Select Tools, Macros    

Step 2:           prev           next


Click to highlight your macro name.
Step 3:           prev
Click run.

Remember, all the simple macro-writing techniques you learned in the OCLC
Connexion Client macro-writing mini-tutorial apply when you write Excel
macros!

Simplified Excel Macros


Example 2 (Insert and name 12
Example 1 Font bold, center text in cell
worksheets)
Example 3 (Create a table)

The macros generated by Excel's macro recorder often contain too many
unnecessary lines. After I use the Excel macro recorder to generate a
macro, I always study the generated code, then eliminate unnecessary
lines.
Excel's macro recorder generated the macro example below to set the
font weight to bold and to center align the text in the selected cell.

Sub boldcenter()
        Selection.Font.Bold = True
        With Selection.Font
                .Name = "Arial"
               .Size = 12
               .Strikethrough = False
               .Superscript = False
               .Subscript = False
               .OutlineFont = False
               .Shadow = False
               .Underline = xlUnderlineStyleNone
               .ColorIndex = xlAutomatic
       End With
       With Selection
               .HorizontalAlignment = xlCenter
               .VerticalAlignment = xlBottom
               .WrapText = False
               .Orientation = 0
               .AddIndent = False
               .IndentLevel = 0
               .ShrinkToFit = False
               .ReadingOrder = xlContext
               .MergeCells = False
        End With
End Sub

ALL I wanted was a macro to make the font bold and center the text! So I
deleted all the unnecessary lines to create the shorter, simpler version
below.

Sub boldcenterevised()
        Selection.Font.Bold = True
        With Selection
                .HorizontalAlignment = xlCenter
        End With
End Sub

To see more examples of generated macros that I have revised to include


only the lines of code I need and nothing more , scroll down ...

Example 2           Back to top of page

The two macros shown below both insert 12 worksheets and name the
worksheets in Fiscal Year order.

But the version on the left is not concise enough for me!

GENERATED VERSION: MY SIMPLE VERSION:

Sub CALENDARSHEETS() Sub CALENDARSHEETSrevised()


' Macro1 Macro
' Macro recorded by Merry Morris ' CALENDARSHEETS Macro
' Macro revised by Merry Morris
        Sheets.Add
       Sheets("Sheet4").Select        Sheets.Add.Name = "JUNE"
       Sheets("Sheet4").Name =        Sheets.Add.Name = "MAY"
"JUNE"        Sheets.Add.Name = "APRIL"
       Range("B13").Select        Sheets.Add.Name = "MARCH"
       Sheets.Add        Sheets.Add.Name =
       Sheets("Sheet5").Select "FEBRUARY"
       Sheets("Sheet5").Name =        Sheets.Add.Name =
"MAY" "JANUARY"
       Range("B7").Select        Sheets.Add.Name =
       Sheets.Add "DECEMBER"
       Sheets("Sheet6").Select        Sheets.Add.Name =
       Sheets("Sheet6").Name = "NOVEMBER"
"APRIL"        Sheets.Add.Name =
       Range("B4").Select "OCTOBER"
       Sheets.Add        Sheets.Add.Name =
       Sheets("Sheet7").Select "SEPTEMBER"
       Sheets("Sheet7").Name =        Sheets.Add.Name = "AUGUST"
"MARCH"        Sheets.Add.Name = "JULY"
       Range("C3").Select         End Sub
       Sheets.Add
       Sheets("Sheet8").Select
       Sheets("Sheet8").Name =
"FEBRUARY"
       Range("B28").Select
       Sheets.Add
       Sheets("Sheet9").Select
       Sheets("Sheet9").Name =
"JANUARY"
       Range("B1").Select
       Sheets.Add
       Sheets("Sheet10").Select
       Sheets("Sheet10").Name =
"DECEMBER"
       Range("C1").Select
       Sheets.Add
       Sheets("Sheet11").Select
       Sheets("Sheet11").Name =
"NOVEMBER"
       Range("C1").Select
       Sheets.Add
       Sheets("Sheet12").Select
       Sheets("Sheet12").Name =
"OCTOBER"
       Range("B1").Select
       Sheets.Add
       Sheets("Sheet13").Select
       Sheets("Sheet13").Name =
"SEPTEMBER"
       Range("B1").Select
       Sheets.Add
       Sheets("Sheet14").Select
       Sheets("Sheet14").Name =
"AUGUST"
       Range("C1").Select
       Sheets.Add
       Sheets("Sheet15").Select
       Sheets("Sheet15").Name =
"JULY"
       Range("B1").Select
End Sub

whew!

Example 3           Back to top of page

The two macros below both create the table shown here:
But the version on the left is needlessly long. So I deleted all of the 74
unnecessary lines to create the simplified version on the right.

GENERATED VERSION (145 lines):

Sub CreateTable()
' CreateTable Macro
' Macro recorded by Merry Morris

Range("A1:G1").Select
Selection.Font.Bold = True
With Selection.Font
        .Name = "Arial"
        .Size = 16
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
End With
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Table Title Goes Here"
Range("A2").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
End With
ActiveCell.FormulaR1C1 = "CALL #"
Range("B2").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
End With
ActiveCell.FormulaR1C1 = "AUTHOR"
Range("C2").Select
ActiveCell.FormulaR1C1 = "TITLE"
Range("C2").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
End With
Range("D2").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
End With
ActiveCell.FormulaR1C1 = "PUBLISHER"
Range("E2").Select
Columns("D:D").EntireColumn.AutoFit
Range("E2").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
End With
ActiveCell.FormulaR1C1 = "ISBN#"
Range("F2").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
End With
ActiveCell.FormulaR1C1 = "OCLC#"
Range("G2").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
End With
ActiveCell.FormulaR1C1 = "DATE"
Range("A1:G20").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
End With
Range("G25").Select
End Sub

whew!

Some of) My Microsoft Excel Macros


created the simple way!

Insert 12 worksheets Adjust column width


Insert and name 12 sheets for FY
Create table with headings and borders
statistics
Select area (with input box) and print
Select area and print selected area
selected area
Select area (with input box) and delete
Select area and delete all entries
all entries
Freeze panes
Sub insertsheets()            Return to top
'MacroName: insert12sheets
'MacroDescription: written by Merry Morris to insert 12 worksheets
        Sheets.Add Count:=12
End Sub

Sub adjustcolumnwidth()             Return to top


'MacroName: adjustcolumnwidth
'MacroDescription: created by Merry Morris to adjust column width
        Columns("A:L").Select
        Selection.COLUMNWIDTH = 10
        With Selection
              .HorizontalAlignment = xlCenter
        End With
End Sub

Sub CALENDARSHEETS()             Return to top


'MacroName: Insert 12 sheets in Fiscal Year order
'MacroDescription: written by Merry Morris to insert and name 12 worksheets
for fiscal year statistics
        Sheets.Add.Name = "JUNE"
        Sheets.Add.Name = "MAY"
        Sheets.Add.Name = "APRIL"
        Sheets.Add.Name = "MARCH"
        Sheets.Add.Name = "FEBRUARY"
        Sheets.Add.Name = "JANUARY"
        Sheets.Add.Name = "DECEMBER"
        Sheets.Add.Name = "NOVEMBER"
        Sheets.Add.Name = "OCTOBER"
        Sheets.Add.Name = "SEPTEMBER"
        Sheets.Add.Name = "AUGUST"
        Sheets.Add.Name = "JULY"
End Sub

Sub CreateTable()             Return to top


'MacroName: createtable
'MacroDescription: written by Merry Morris to create a table with headings
and borders
Range("A1:G1").Select
Selection.Font.Bold = True
With Selection.Font
          .Name = "Arial"
          .Size = 16
End With
With Selection
          .HorizontalAlignment = xlCenter
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Table Title Goes Here"
Range("A2").Select
With Selection
          .HorizontalAlignment = xlCenter
End With
ActiveCell.FormulaR1C1 = "CALL #"
Range("B2").Select
With Selection
          .HorizontalAlignment = xlCenter
End With
Range("B2").Select
ActiveCell.FormulaR1C1 = "AUTHOR"
Range("C2").Select
ActiveCell.FormulaR1C1 = "TITLE"
Range("C2").Select
With Selection
          .HorizontalAlignment = xlCenter
End With
Range("D2").Select
With Selection
          .HorizontalAlignment = xlCenter
End With
ActiveCell.FormulaR1C1 = "PUBLISHER"
Columns("D:D").EntireColumn.AutoFit
Range("E2").Select
With Selection
          .HorizontalAlignment = xlCenter
End With
ActiveCell.FormulaR1C1 = "ISBN#"
Range("F2").Select
With Selection
          .HorizontalAlignment = xlCenter
End Withv ActiveCell.FormulaR1C1 = "OCLC#"
Range("G2").Select
With Selection
          .HorizontalAlignment = xlCenterv End With
ActiveCell.FormulaR1C1 = "DATE"
Range("A1:G20").Select
With Selection.Borders(xlEdgeLeft)
          .LineStyle = xlContinuous
          .Weight = xlThick
End With
With Selection.Borders(xlEdgeTop)
          .LineStyle = xlContinuous
          .Weight = xlThick
End With
With Selection.Borders(xlEdgeBottom)
          .LineStyle = xlContinuous
          .Weight = xlThick
End With
With Selection.Borders(xlEdgeRight)
          .LineStyle = xlContinuous
          .Weight = xlThick
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
End Sub

Sub freezepanes() Return to top


'MacroName: freezepanes
'MacroDescription: written by Merry Morris to freeze panes above and/or to
the left of selected cell
ActiveWindow.freezepanes = True
End Sub
Sub printselection() Return to top

'MacroName: printselection
'MacroDescription: written by Merry Morris to select area of worksheet and
print selected area
Range("A1:C30").Select
Selection.PrintOut
End Sub

Sub printselection2() Return to top


'MacroName: printselection2
'MacroDescription: written by Merry Morris to select area of worksheet (from
user input box) and print selected area
Dim selectedarea As String
selectedarea = InputBox("Select Range","Enter range","A1:C30">
Range(selectedarea).Select
Selection.PrintOut
End Sub

Sub DeleteEntries() Return to top


'MacroName: deleteentries
'MacroDescription: written by Merry Morris to select area of worksheet and
delete all entries
Range("A1:I4").Select
Selection.ClearContents
End Sub

Sub DeleteEntries2() Return to top


'MacroName:deleteentries2
'MacroDescription: written by Merry Morris to select area of worksheet (with
input box) and delete all entries
Dim selectedarea As String
Range(selectedarea).Select
Selection.ClearContents
End Sub

You might also like