How to record a macro in Excel 2007

If you have a task that you perform repeatedly, then you can record all the events to achieve that task (keystrokes, mouse clicks … etc.) in a macro.

To record a macro in Excel 2007 follow these steps.

1. Click on the Developer tab. If the Developer tab is not visible, then check this post on how to show the Developer tab:

2. In the Code group Click Record Macro.

xl7macros1

3. The Record Macro window will open.

xl7macros2

4. Give a name to your macro.

5. Specify a shortcut key(optional).

6. Specify where you want to save the macro.

7. Click OK.

8. Now start performing the task which you want to record. When you are done click Stop Recording.

xl7macros3

9. Your macro is now created and is ready to be used. To run the macro:

  1. Click Macros.
  2. xl7macros4

  3. The Macros window will open.
  4. xl7macros5

  5. Select the macro then click run.

10. You can also run the macro by simply clicking the shortcut key combination.

A macro to merge worksheets from two workbooks into a new workbook

If you have two workbooks, or more, and you want to merge or consolidate those workbooks into one, then you may find it a tedious task to move the worksheets manually.

The following macro reads two workbooks and copy all the worksheets into a new consolidated workbook. If you have more than two workbooks as input then you can modify the macro accordingly.

Sub MergeWorkbooks()
‘**********************************************************
‘ Merge worksheets from two workbooks into a new workbook *
‘**********************************************************
Dim OldBook1 As Workbook
Dim OldBook2 As Workbook
Dim NewBook As Workbook
Dim wSheet As Worksheet
‘ Stop screen flicker and speed up the execution of the macro
Application.ScreenUpdating = False
‘ Create a new workbook ( the destination for merging the old ones)
Workbooks.Add
Set NewBook = ActiveWorkbook
‘ Assign the old workbooks (must be open before running the macro)
Set OldBook1 = Workbooks(“TestBook1.xls”) ‘ Put the required file name here
Set OldBook2 = Workbooks(“TestBook2.xls”) ‘ Put the required file name here
‘ Loop thru the first workbook an copy its worksheets to the destination workbook
For Each wSheet In OldBook1.Worksheets
wSheet.Copy After:=NewBook.Worksheets(NewBook.Worksheets.Count)
Next
‘ Loop thru the second workbook an copy its worksheets to the destination workbook
For Each wSheet In OldBook2.Worksheets
wSheet.Copy After:=NewBook.Worksheets(NewBook.Worksheets.Count)
Next
‘ Save the destination workbook
NewBook.SaveAs Filename:=”C:/TestMerg2.xls” ‘ Put the required path and file name here
‘Set screen updating back to normal
Application.ScreenUpdating = True
End Sub

This code is also available in the Downloads page.

Copy the code to a new module in Visual Basic Editor, then run it like any other VBA macro.

Suggested Post:
How to write a VBA macro

Applies to: Excel 2003

How to determine if a cell has a formula or not

If you want to determine whether a specific cell has a formula or a value then you need to check a range property called “HasFormula”. Here is an example where the border color of a cell is changed to red if it has a formula, otherwise it is set to black.

Dim myRange As String
myRange = “B5″
If ActiveSheet.Range(myRange).HasFormula Then
ActiveSheet.Range(myRange).Borders.Color = RGB(255, 0, 0)
Else
ActiveSheet.Range(myRange).Borders.Color = 0
End If

Suggested Post:
How to write a VBA macro

A macro to print only hidden sheets in a workbook

If you have a workbook with a number of worksheets, some visible and other hidden, and you want to print only the hidden sheets, then you can use the macro shown below.
Because Excel will not allow the macro to be printed while it is hidden, this macro will make the sheet visible, print it and then hide it again.

Sub PrintHiddenSheets()
‘*********************************************************
‘ Print only hidden sheets in the active workbook *
‘*********************************************************
Dim wSheet As Worksheet
Dim CurStat As Variant
For Each wSheet In ActiveWorkbook.Worksheets
If Not wSheet.Visible Then
CurStat = wSheet.Visible
wSheet.Visible = xlSheetVisible
wSheet.PrintOut
wSheet.Visible = CurStat
End If
Next
End Sub

This code is also available in the Downloads page.

Copy the code to a new module in Visual Basic Editor, then run it like any other VBA macro.

Check this post if you want to know How to write a VBA macro.

A macro to print only visible sheets in a workbook

If you have a workbook with a considerable number of worksheets, some visible and other hidden, and you want to print only the visible sheets, then you can use this macro:

Sub PrintVisibleSheets()
‘***************************************************
‘Print only visible sheets in the active workbook *
‘***************************************************
Dim wSheet As Worksheet
For Each wSheet In ActiveWorkbook.Worksheets
If wSheet.Visible Then wSheet.PrintOut
Next
End Sub

This code is also available in the Downloads page.

Copy the code to a new module in Visual Basic Editor, then run it like any other VBA macro.

Check this post if you want to know How to write a VBA macro.

How to write a VBA macro

If you know Visual Basic programming then you can create your own VBA macro and run from within Excel.

Follow the steps below:

1. On the Tools menu select Macros.

2. On the Macro submenu select Visual Basic Editor.

User defined1

3. You will be switched to Visual Basic Editor Menu.

User defined 2

4. On the Insert menu (Visual Basic Editor) select Module.

5. This will open the code window.

User defined 3

6. Type the Visual Basic code for your macro.

Here is a simple example. A macro to increment the variable i by 5 and write it to column 2 (Column B) in the next row :

Sub TestMacro()
Dim i As Integer, Row As Integer, Col As Integer
i = 0
Col = 2
For Row = 1 To 10
i = i + 5
ActiveSheet.Cells(Row, Col).Value = i
Next Row
End Sub

7. On the File menu click Close and return to Microsoft Excel.

8. Your new macro is now ready to be run.

To run your macro:

1. On the Tools menu select Macros. A list of available macros will be displayed.

2. Select your macro and click Run.

Applies to: Excel 2003

Macro based yearly calendar template

I have added to the downloads page a yearly calendar template. This is a dynamic template that will allow you to generate a calendar for any year between 2001 and 2016. There are two combo boxes for the user to select the year and the start date of the week. Each selection will trigger a macro that will generate the calendar. Here are a few notes to consider:

To Enable Macros:

Select : Tools —> Macro —> Security —> Security Level —> Medium

Names of days and months

I have put the names of days and months as tables in the “Settings” worksheet. This will allow you to translate them in your own language if necessary. You have to unprotect the sheet first before you can do that.

Protection

1. All sheets are protected to avoid accidental change of data and formulas. If you want to make some changes like formatting for example, then you can unprotect the sheet. Make sure however that you don’t mess up the formulas.
2. Columns X and Y are hidden but not protected. These columns receive data from the two combo boxes for the year and the start date.

Printing

You can print the calendar just like any other Excel worksheet. The print area is set in a way that it will only print the year and the 12 boxes for the months, i.e. without the combo boxes.

VBA code

If you want to see the code behind all this, then switch to visual basic editor. The main routines are in module2, but some of the code is within different events of the workbook and the combo boxes.

Your feedback is appreciated.

Applies to: Excel 2003

Function to return the address of a lookup value

If you look at many of Excel lookup functions you will find out that they either return a value or a position of an item. There is no function that returns the address where a lookup value is found.

Below is a code for a user defined function that takes two arguments: a lookup value and a range, and returns the address where the lookup value is found within the range. If the lookup value is not found, it will return the text “#N/A”.

You can download the function as a text from the Download Page.

If you need more help on how to create a user defined function click here.

Function LookupAddress(LookupVal As String, TheRange As Range)
Dim Cell As Range, Found As Boolean
For Each Cell In TheRange.Cells
If Cell.Value = LookupVal Then
LookupAddress = Cell.Address
Found = True
Exit For
End If
Next Cell
If Not Found Then LookupAddress = “#N/A”
End Function

Applies to Excel 2003

Macro based monthly calendar template

I have added to the downloads page a dynamic monthly calendar template. There are three combo boxes for the user to select the month, year and the start date of the week. Each selection will trigger a macro that will generate the calendar. Here are a few notes to consider:

To Enable Macros:

Select : Tools —> Macro —> Security —> Security Level —> Medium

Proverbs

1. This calendar uses function RANDBETWEEN from the analysis toolpack to generate a random number for the proverb lookup.
2. Every time recalculation occurs you will get a different proverb (hit PF 9 to recalculate)
3. If you don’t have the analysis toolpack then copy the following formula in cell “A12″ and remove the quotes. You will get a proverb depending on the selected month.
“=VLOOKUP(I2,Settings!$C$2:Settings!$D$13,2,FALSE)”
4. The proverbs are stored in the “Settings” worksheet. If you don’t like my selection then you can replace them with your own.

Names of days and months

I have put the names of days and months as tables in the “Settings” worksheet. This will allow you to translate them in your own language if necessary. You have to unprotect the sheet first before you can do that.

Protection

1. All sheets are protected to avoid accidental change of data and formulas. If you want to make some changes like formatting for example, then you can unprotect the sheet. Make sure however that you don’t mess up the formulas.
2. Columns H and I are hidden but not protected. These columns receive data from the three combo boxes for the month, year and start date.

VBA code

If you want to see the code behind all this, then switch to visual basic editor. The main routines are in module2, but some of the code is within different events of the workbook and the combo boxes.

Your feedback is appreciated.

Applies to: Excel 2003

How to make your macros and functions available to all workbooks

When you create a macro or a user defined function in a workbook, by default that macro will only be visible for that specific workbook.
If you have a code that you use repeatedly and you want it to be available for all workbooks then you have to create a global workbook called Personal.xls and save it in Excel startup folder.

How to create the file:

1. Open a new workbook.
2. Save As Personal.xls in folder:
C:\Program Files\Microsoft Office\OFFICE11\XLSTART
3. Copy your macros from their original workbooks to personal.xls.
4. Click Window –> Hide to hide the file.

The path may differ slightly depending on which version of office you are using.

How to use the file:

1. Whenever you start Excel personal.xls will be opened automatically, and all your macros will be available for use in any workbook.

2. Keep personal.xls hidden unless you want to add macros to it.

3. When you want to add new macros Click Window –> Unhide and select “personal.xls”.

4. To add a new macro click Tools –> Macro –> Record new macro. You will get the following dialogue box.

savemacro

5. Under “Store macro in:” select “Personla.xls”.

6. When you are done hide the file again.

If you are interested in more details please see the following Microsoft office link:

Deploy your Excel macros from a central file

Applies to: Excel 2003