Popup Calender in Excel
Popup Calender in Excel
One of the biggest problems in maintaining "good" data is the entry of dates. People seem to get
confused about entering dates. Should they enter dd/mm/yy or mm/dd/yy? Do they enter slashes or
dashes or dots? And what was the date of the third Thursday in September last year anyway? What
NOTE: When I first wrote this tutorial I made use of the Microsoft Calendar Control, an ActiveX
control that was installed along with Microsoft Office Professional (i.e. the version that included
Microsoft Access). If you didn't have that version of Microsoft Office you could still download and
install a copy of the ActiveX control. If you want to do that you can follow the original tutorial here.
Since then Microsoft have created an alternative to the Calendar Control called the MonthView
control. This new ActiveX control is similar in function to the Calendar Control and has the advantage
that it is installed with all versions of Microsoft Office. In Microsoft Office 2007 you have the choice of
which ActiveX control to use but in Microsoft Office 2010 you must use the MonthView control as
The tutorial shows you how to create a pop-up calendar using the Microsoft MonthView control that is
installed with Excel. You will use the Visual Basic Editor to create a UserForm that displays a
calendar. You will also write some VBA code to power the UserForm and to generate an additional
item on the menu that appears when the user right-clicks a cell on an Excel worksheet. Clicking the
menu item will display the calendar. When the user selects a date it is automatically entered into the
month in calendar format (it can be set to display more than one if required). Either side of the
month name is an arrow button which displays the previous or next month when clicked. Clicking on
the month name opens a list of months so that you can quickly jump to a specific month. Clicking on
the year number reveals a spinner which lets you change the year. The current date is always shown
at the bottom of the calendar. Click it to jump to today's date on the calendar.
creates and operates the calendar has to reside inside an Excel file. The question is which one? If you
want the pop-up calendar to be available whenever you are working in Excel you should create it
in Personal.xlsb, sometimes referred to as your Personal Macro Workbook, because this file exists to
store code that you want to be available to all your Excel files. Personal.xlsb opens and is hidden
each time Excel is started. Any macros and functions it contains are then available for use in any
other workbook. To find out whether or not you already have a copy of Personal.xlsb read the
However since the Personal Macro Workbook, as its name implies, is specific to your copy of Excel it
will reside either on your computer's hard drive or within your personal profile on the network. It isn't
the best place to put the code if you want other users to have access to the pop-up calendar.
Instead, you could create your pop-up calendar in a specific workbook. It would be available
whenever (and only when) that workbook was open, but also available to any other workbook that
was open at the same time. A pop-up calendar created in an Excel template would be present in each
workbook that was generated from the template. The most flexible option is to create an Excel Add-
In. It's easy to do and will allow you to distribute your calendar to other users. You start by building
the calendar exactly as described in this tutorial but in a new Excel workbook(not Personal.xlsb)
which you then convert to an Excel Add-In. The last section of this tutorial takes you through the
In this tutorial I will be using Personal.xlsb but if you choose to put your pop-up calendar somewhere
Developer tab. It offers quick access to many of the tools you will be using during your projects. If it
isn't currently visible in your copy of Excel you can enable it by going to Excel Options.
If you have never recorded a macro on your current copy of Excel then you probably won't have a
Visual Basic Editor. If you don't have one it takes just a moment to create: On the Developer tab
click the Record Macro button. When the Record Macro dialog appears choose to store the macro
the Developer tab. You have just recorded an empty macro but that action was sufficient to prompt
Excel to create a copy of Personal.xlsb to store it in. You can delete the macro later but now you
that will make it work. If you plan to create an Excel Add-In you should open a new empty workbook.
If you want your calendar to reside in a specific workbook then make sure that workbook is open. In
Excel open the Visual Basic Editor using the keyboard shortcut [ALT]+[F11] or click the Visual
IMPORTANT: Remember to save your work regularly. Since a UserForm and its associated code
resides within an Excel workbook the they are saved when you save the workbook and vice-versa.
You can save from within Excel or from within the Visual Basic Editor window in the usual way by
clicking the Save button. Excel will warn you if there are any unsaved changes when you close the
workbook. If you are working in a regular Excel workbook (and not in Personal.xlsb) you should save
your workbook as an Excel Macro Enabled Workbook (*.xlsm). Failure to do this will result in
build and program with VBA. Go to the Project Explorer window of the Visual Basic Editor. The
Project Explorer is usually located in the upper left corner of the Visual Basic Editor window. If you
can't see it, switch it on from the View menu. Right-click on the name of the workbook in which
A new empty UserForm will appear in the main window of the Visual Basic Editor together with the
Toolbox containing buttons for the most commonly used objects (called controls) that can be placed
on a form. You can switch the Toolbox on and off from a button on the toolbar. You will also notice
that the Toolbox disappears when the UserForm is not selected. If this happens just click on the
When the UserForm is selected the Properties Window of the Visual Basic Editor displays a list of
all the UserForm's properties. The Properties Window is normally located in the lower left corner of
the Visual Basic Editor window. If you can't see it switch it on from the View menu. Excel
automatically names and captions (where appropriate) new objects. You will see that the UserForm
has been given the name and caption UserForm1. It is good practice to give objects more meaningful
IMPORTANT: If you choose to use different names from those suggested here remember to modify
You can test the Userform from the Visual Basic Editor now (and at any stage) by pressing [F5] on
your keyboard or clicking the Run button on the toolbar. Doing this opens the UserForm in Excel so
that you can check the progress of your design. Closing the UserForm (click the [X] in its upper-right
right corner of the UserForm, and our code will close the form automatically after a date has been
chosen. But doing so will add a useful feature that most users take for granted, that is to close the
Click the CommandButton button on the Toolbox then click on the center of the UserForm. The dots
on the UserForm represent a grid to help you align objects neatly. Point at the dotted border of the
new command button and drag it so that it is located two grid points down from the top and two grid
Now use the Properties Window to change the command button's Name property to cmdClose,
the Cancel property of a command button is set to True the button gets clicked automatically when
In this step you will attach some code to the Cancel button so that when it is clicked the UserForm
will close, thus closing the pop-up calendar. In the Visual Basic Editor double-click the Close
command button. This opens the UserForm's code window with an empty Event Procedure for
the Click event of the command button (an Event Procedure is a collection of commands that are
executed when a particular event happens - in this case when the cmdClose button is clicked).
Unload Me
This instructs Excel to close the UserForm. The completed code should look like this:
Private Sub cmdClose_Click()
Unload Me
End Sub
Return to the UserForm design window by double-clicking its name in the Project Explorer or by using
the keyboard shortcut [CTRL]+[TAB] to switch windows, then test the UserForm as before by
pressing [F5] or clicking the Run button on the toolbar. You should be able to close the UserForm
from Excel by either clicking the Close button or by pressing the [ESC] key.
open in memory so that it can be reopened without losing its data). The word Me is a
quick way of referring to the current UserForm without having to use its full name. I
could have written Unload frmCalendar. Since the form is usually referred to frequently in
the code using Me simplifies code writing and saves a lot of editing if the UserForm's
The MonthView control will provide the calendar that you will place on the UserForm. It is not
normally present on the Toolbox so you will have to ask for it. Right-click on the Toolbox and
choose Additional Controls. Scroll down the list in the Additional Controls dialog until you
find Microsoft MonthView Control 6.0 (the version number might be different depending on your
version of Microsoft Office) and check the box next to its name, then click OK to close the dialog. You
will see that a button for the new control has been added to the toolbox.
Click the MonthView button on the Toolbox then click on the UserForm to create a calendar on the
form. Drag the calendar into the upper left corner to the UserForm. The calendar will cover the Close
button you created earlier. This is intentional because the Close button will be operated by the user
When the MonthView control is selected the Properties Window displays its properties. By default it
has a sunken border. I don't like this effect so I have changed the Appearance property to 0 -
The ShowToday property lets you choose whether or not to highlight and display the current date.
Step 7: Resize the UserForm
The final step in designing the calendar is to change the size of the UserForm to match the
dimensions of the calendar. Click on the background of the UserForm to select the form then use the
resizing handles (white rectangles located around the dotted border) to drag the edges of the
UserForm to the required size. Finally, run the form to check that it looks OK.
In this step you will create the code that writes a date on to the worksheet when the user clicks one
of the day buttons on the calendar. Double-click the MonthView control to open the code window.
This opens the UserForm's code window with an empty Event Procedure for the DateClick event of
You have a couple of choices now depending upon what you want to happen when the user chooses a
date. If you want the calendar to enter the chosen date only in the active cell, which is the selected
cell when only a single cell is selected or only the active cell if a block of cells or multiple cells are
selected, then use this method. Between the Sub...and End Sub lines enter the statements:
On Error Resume Next
ActiveCell.Value = DateClicked
Unload Me
This instructs Excel to enter the chosen date into the currently active cell and the close the UserForm.
If you want the calendar to enter the chosen date in all the currently selected cells, whether this is a
block of cells, a multiple selection, or just a single cell if only one is selected, then use this method.
This code is more versatile so I would probably do it this way unless I had a particular reason not to
cell.Value = DateClicked
Next cell
Unload Me
This instructs Excel to write a date into each of the selected cells. Your code should look like this:
Test the code by running the calendar as before, with one or several cells selected. If it fails to work
Although this is a very simple operation, in both cases I have added the statement On
Error Resume Next which tells Excel to ignore any error that might occur. This will
prevent the code from crashing if, for example, the selected cell or one of the cells in a
selection is locked. The code makes use of the DateClicked parameter which is given the
date chosen when the user clicks one of the day buttons. In the first example that value
is simply written into the active cell before the UserForm is closed. In the second
example the code first declares a variable that represents a single cell. It then employs
a For...Next code loop to visit each cell in the current selection, entering the value into
I nearly didn't include this step, which is optional anyway, because whilst it works perfectly on the old
version of my pop-up calendar it doesn't work completely as it should in this version. The idea is
that, if the active cell already contains a date, the calendar opens to display that same date. I have
been able to make the calendar display the correct month and year but despite my best efforts can't
get it to highlight the specific day. The MonthView control has a DayBold property which is
"supposed" to be controllable with code - you tell the calendar which day number to display in bold
and it does so - but in the current context I just can't make it work. I've searched for a solution
without success so if you find one please let me know! Here's how to have the calendar open at the
Open the UserForm's code window by right-clicking on its name in the Project Explorer and
choosing View Code or pressing the [F7] key. At the top of the code window are two drop-down
lists. From the left-hand list choose UserForm the from the right-hand list choose Initialize. This
also created you can delete it). Between the Sub... and End Sub statements type:
If IsDate(ActiveCell.Value) Then
Me.MonthView1.Value = ActiveCell.Value
End If
Private Sub UserForm_Initialize()
If IsDate(ActiveCell.Value) Then
Me.MonthView1.Value = ActiveCell.Value
End If
End Sub
Test the code in different scenarios: by opening the calendar when the active cell in Excel is empty;
when it contains something that is not a date (a number or some text); and when it contains a date
different from the current date. If the active cell is empty, or contains text or a number, the calendar
should open to show the current date. If the active cell already contains a date the calendar should
The Initialize event happens as the UserForm opens. It is used to prepare the form for
use with such tasks as filling lists and setting starting values for textboxes. Here it is
being used set the initial value of the MonthView control. The code uses
the IsDate function to check whether or not the active cell contains a date. This is used
as the condition for an If Statement so that, if the function returns True, the value of the
calendar is set to the same date. If the function returns False then no date was found so
nothing changes and the calendar opens with its default date.
Until now you have been opening the pop-up calendar from from the Visual Basic Editor but we need
a way to open it from Excel. This will take the form of a simple macro. In the Project Explorer right-
click on the name of the workbook in which you created the UserForm then
choose Insert and Module. This creates a new code module which then opens in the main window of
Sub OpenCalendar
and press [Enter]. Excel places a pair of brackets after your typing and after an empty line adds the
line End Sub. Place your cursor in the empty line between Sub... and End Sub and enter the
following statement:
frmCalendar.Show
If you gave your UserForm a different name make sure you use it here. The completed code should
You can test your macro by going to Excel and clicking the Macros button on the View tab or
the Developer tab. Your macro will be listed in the Macro dialog box. Select it then click Run to
you can't use Me to refer to the UserForm because it is not the object in which this bit of
a Load command which opens a Userform into memory but does not make it visible. It is
unnecessary to use it here because, if a UserForm is not already open in memory when
automatically.
Now that there is a macro to open the calendar we can add some features to make it more
convenient for the user to run it. One way is to add a new command to the Cellcontext menu, the
one that the user sees after right-clicking on a cell. This means writing some code to add a new item
to the menu when the file containing the calendar opens, and some more code to remove the menu
item when the file closes. This code has to run automatically when the file opens or closes so we
make use of special event procedures that are located in the ThisWorkbook code module.
In the Visual Basic Editor go to the Project Explorer and click the plus sign [+] next to the folder
marked Microsoft Excel Objects under the name of the file containing your calendar. When the
folder opens double-click the item marked ThisWorkbook to open the code module in the main
window. Choose Workbook from the left-hand drop-down list at the top of the window. This
Application.CommandBars("Cell").Controls("Insert Date").Delete
With NewControl
.OnAction = "Module1.OpenCalendar"
.BeginGroup = True
End With
Private Sub Workbook_Open()
On Error Resume Next
Dim NewControl As CommandBarControl
Application.CommandBars("Cell").Controls("Insert Date").Delete
Set NewControl = Application.CommandBars("Cell").Controls.Add
With NewControl
.Caption = "Insert Date"
.OnAction = "Module1.OpenCalendar"
.BeginGroup = True
End With
End Sub
IMPORTANT: If you changed any of the names within your project you must make sure you also edit
the code so that the matching names are used. So, if you called your macro anything other than
"OpenCalendar" or the module in which it is located is called anything other than "Module1", you
should amend the code accordingly. Make sure, for example, that the text "Insert Date" is spelled the
same way each time it is used. Simple errors like this are the most common reason for code not
working
before, the code starts with an instruction to ignore any errors that might occur (you
should always use this command with caution and only when you are sure that nothing
proceeding to create the new menu item the next statement deletes the item from the
menu. This seems illogical but it is a safety measure to make sure that, if Excel did not
close properly last time, we are not left with multiple copies of the menu item. If, as
expected, Excel closed properly and the menu item was deleted then this statement
would cause an error to occur, hence the error handler at the start. The next statement
adds the item to the menu. A With Statement (a way of grouping together a number of
commands relating to the same thing) is used to set the properties of the new menu
item.
Now go to the right-hand drop-down list at the top of the code window and choose BeforeClose to
Sub statements type:
Application.CommandBars("Cell").Controls("Insert Date").Delete
procedure simply deletes the new item from the menu and is protected with a simple
error handler.
If you like using keyboard shortcuts then you might like to have one automatically assigned to your
You also need to cancel the shortcut assignment when the file containing the calendar closes so add
Application.OnKey "+^{C}"
represents the [SHIFT] key, the caret (^) represents the [CONTROL] or [CTRL] key, and
the {C} represents the letter C. If you prefer a different shortcut then you can specify
something else. If you would like to see a complete list of the key codes that can be used
go to your code module and place you cursor within the word OnKey in the code, then
time without the macro assignment, to cancel the assignment when it is no longer
needed.
Before testing your code you should CHECK YOUR TYPING, especially anything you have typed in
quotes, such as the caption you used for the menu item! The Visual Basic Editor is good at spotting
coding errors but can't check your text entries. It is far better to find and correct any errors now than
after your code has crashed and maybe caused problems in your worksheet. You can additionally
check any new code by compiling it. Open the Debug menu and choose Compile VBA Project. If
you get no messages then the editor has not found any problems. If the Compile... entry is disabled
Compile and Save your code then close Excel. This ensures that Personal.xlsb is closed. If you are
working in a file other than your Personal.xlsb then you need only close and save that file. Re-open
Excel (or re-open the file in which you created the calendar if it was not Personal.xlsb) and right-click
on any cell. You should see the command Insert Date at the bottom of the context menu. Choose it
to make your calendar appear. When you pick a date from the calendar the date will be written into
the cell and the calendar will close. If you added the keyboard shortcut to open the calendar then
test that too by holding down the [CONTROL] and [SHIFT] keys and pressing [C].
If any of this does not work then the most likely problem is a typing error in your code. Go back and
check it thoroughly!
If it is likely that other users will have access to your code projects and particularly if you intend to
distribute your project either as a shared file, template or Add-In, it is advisable to protect your code
with a password. This has a number of advantages. It prevents a third party from viewing and
possibly interfering with the code. It also guarantees that, in the unfortunate event that an
unforeseen error causes the code to crash, the user does not find themselves in the Visual Basic
Editor looking at a stalled code module and not knowing what to do next.
In the Visual Basic Editor go to the Project Explorer then right-click on the name of the file containing
the Properties dialog and enter then confirm your password. Place a tick in the Lock project for
If you try to view the code of a protected code project the Visual Basic Editor requires you first to
IMPORTANT: Don't forget the password! If you lose the password to a VBA code project you will
permanently lock yourself out of the code. There is no easy way to recover the password of a
That's it! You've finished and should have a fully-functioning pop-up calendar. If you want to continue
distribute to other users. If you have been working so far in Personal.xlsbyou don't need to build the
calendar again. Just copy or move your UserForm and associated code into a regular Excel workbook.
It isn't difficult to do. Open a new, empty workbook in Excel then in the Visual Basic Editor go to the
Properties Window and drag the UserForm from Personal.xlsb to your new workbook where Excel will
create a copy of it. All the UserForm's code will automatically go with it. Do the same for the module
in which you created the macro that opens the calendar (Module1 in this example). Then copy
IMPORTANT: If you plan to use the Add-In yourself you should delete the UserForm and all the
associated code from Personal.xlsb to eliminate any risk of conflicts between them.
If you have been working in a regular Excel workbook then you don't need to do anything else, just
If you have been working in a regular Excel workbook you will already have saved the file. If not, or
if you have just copied everything into a new workbook from Personal.xlsbthen you need to save it
now. Save the file as an Excel Macro Enabled Workbook (*.xlsm) and call it something
like Calendar.xlsm. It is possible to miss out this step but I recommend it because it gives you a
back-up copy of your work and also allows you to add a meaningful name and a description to your
Use Windows Explorer to find the workbook you just saved then right-click on it and
information about your Add-In. Alternatively you can you can access the file properties from within
The Title property will become the name of the Add-In (as distinct from its filename) and
the Comments property will be used to provide the user with a brief description of its features. After
To create the Add-In open your Calendar.xlsm file in Excel then open the Save As dialog box:
If you want to give your Add-In a different filename you can do it now. Open the Save as
type dropdown and choose Excel Add-In (*.xlam) from the list then click Save. Excel creates a
copy of the file (the original remains unchanged) as an Add-In and automatically stores it in the Add-
Ins folder. The location of the Add-Ins folder might be different depending on your particular set-up
where to find an Add-In if it is not in the usual place. This is useful if, for example, you want to store
NOTE: You will see that there is also the option to save the file as an Excel 97-2003 Add-In (*.xla).
Don't do this unless you know that it is going to be used in an older version of Excel (Excel 2003 or
earlier). The Add-In needs to be in the right format for the version of Excel in which it will be used.
Now that you have built the Add-In it must be activated in Excel before you can use it. If you are
going to distribute your Add-In the users will need to do this on each computer in which the Add-In is
installed.
Go to the Add-Ins section of the Excel Options dialog. If you have installed the Add-In file in the
default Add-Ins folder you will see it listed here under the Inactive Application Add-Ins heading
(don't worry if you put it somewhere else - you will be able to browse for it later). You can click on its
name here to view details of its location and the properties you assigned earlier.
Make sure that Excel Add-Ins is selected in the Manage list then click Go to open the Add-
Ins dialog in Excel. If you installed the Add-In in the default Add-Ins folder it will be listed in the
dialog box. Otherwise, click the Browse button to locate and select your Add-In file which will appear
in the dialog when you have done so. You will also see any other Add-Ins that have already been
installed including those that come pre-installed with Excel. Selecting one displays its features at the
bottom of the dialog box. Now that Excel knows about your Add-In you can switch it on and off from
here. Doing so effectively opens and closes the Add-In file in Excel. To switch your Add-In on place a
The pop-up calendar is now ready for use and will remain so each time you use Excel until you
deactivate it by un-checking the box next to its name in the Add-Ins dialog box. If you return to
the Add-Ins section of the Excel Options dialog you will see that your Add-In now appears under the
Downloads page. If you download a file from this site it is assumed that you have read, understood
and agreed to the terms set out in the notices.
So, maybe you are too busy or just can't be bothered to build one for yourself. Don't worry, you can
download a ready-made copy of the Pop-up Calendar Add-In or an Excel workbook containing a
functioning Pop-up Calendar right here. The files are not password protected so you can view and
modify the code if you wish. You are strongly advised to password protect the code as described
The files are supplied in .zip format. To download a file right-click on the link and choose Save
target as... (in Internet Explorer) or Save Link As... (in Firefox) and save the .zip file to your hard
disk. You should extract the Add-In or workbook from the .zip file before using it. Right-click on
to