Вы находитесь на странице: 1из 4

Use the Calendar control to fill in dates

Ron de Bruin (last update 14-Dec-2007)


Go back to the Excel tips page
Note: See also the links in the "More Information"section of this page.
How do I insert a Calendar control in my worksheet?
Excel 97-2003
Use Insert-Object on the Worksheet Menu Bar.
Select the control in the list and press OK.
Excel 2007
On the Developer tab use Insert > ActiveX Controls.More controls.
Select the control in the list and press OK
Click on a cell on your worksheet.
Press the "Design Mode" button next to the "Insert button" to turn of Design Mode.
To display the Developer tab go to Office Button >Excel Options...Popular
Excel 97-2007
It is possible you dont see it in the list, because it is installed with Access.
So if you dont have that program installed you possible dont have the control.
You can download the control if you dont have it (See link on the bottom of this page).
Note 1: If you protect your sheet in Excel 97-2000 then you must format the range first with
the Date format you want and remove this line ActiveCell.NumberFormat = "mm/dd/yyyy"
In Excel 2002 and up you be able to protect your worksheet and allow Format cells.
Note 2: This code is not working if there are Merged cells in the range, but you can use this:
Excel 97-2003: Format>Cells>Alignment Tab ... Center Across Selection
Excel 2007: In the Cells group on the Home tab use
Format>Format Cells>Alignment Tab ... Center Across Selection
Where do I copy the code?
You must copy the code in the Worksheet module.
Use the Calendar control to fill in dates http://www.rondebruin.nl/calendar.htm
1 of 4 3/29/2010 12:39 AM
Right click on the sheet tab and choose view code.
Paste the code in the sheet module that is active now and press Alt-Q to go back to Excel.
Private Sub Calendar1_Click()
ActiveCell.Value = CDbl(Calendar1.Value)
ActiveCell.NumberFormat = "mm/dd/yyyy"
ActiveCell.Select
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("A1:A20"), Target) Is Nothing Then
Calendar1.Left = Target.Left + Target.Width - Calendar1.Width
Calendar1.Top = Target.Top + Target.Height
Calendar1.Visible = True
' select Today's date in the Calendar
Calendar1.Value = Date
ElseIf Calendar1.Visible Then Calendar1.Visible = False
End If
End Sub
If you select a cell in the Range A1:A20 the Calendar will popup and when
you Click on the calendar the date will be placed in the active cell.
If you select a cell outside the range the Calendar will disappear.
Note: You can use this if your range is not one area
If Not Application.Intersect(Range("A1:A20,C1,E1"), Target) Is Nothing Then
Tip 1: if the cell value is a Date and you want that the Calendar popup with that date selected
you can replace this line Calendar1.Value = Date for
If Not IsDate(Target.Value) Then
Calendar1.Value = Date
Else
Calendar1.Value = Target.Value
End If
Tip 2: If you want that the calendar disappear when you click on a date in the control you have two options.
After ActiveCell.Select in the Calendar_Click event add this line.
Calendar1.Visible = False
Or select a cell next to the date cell, Replace ActiveCell.Select for:
ActiveCell.Offset(0, 1).Select
Week number code
You can use the code below if you also want to insert the week number in the active cell.
The examples use the DblClick event to insert the week number and week day in the cell.
Excel Week Number / Week Day
If you also want a to be able to use the Calendar control to add the Week Number
and Week Day in the active cell you can copy this event also in the sheet module.
Private Sub Calendar1_DblClick()
Use the Calendar control to fill in dates http://www.rondebruin.nl/calendar.htm
2 of 4 3/29/2010 12:39 AM
'This will insert the Weeknumber / Weekday in the cell
ActiveCell.Value = "W/D " & VBAWeekNum(Calendar1.Value, 1) & " - " & _
Application.WorksheetFunction.Weekday(Calendar1.Value, 1)
ActiveCell.Select
End Sub
If you DblClick on the Calendar the Week Number and Week Day will be placed
in the active cell. You also need the function below to use this.
Alt-F11
Insert>Module in the menu bar
paste the Function in this module
Alt-Q to go back to Excel
Function VBAWeekNum(D As Date, FW As Integer) As Integer
VBAWeekNum = CInt(Format(D, "ww", FW))
End Function
ISO Week Number / Week Day
If you want to use the ISO week number instead of the Excel week number then use the following code.
Copy this in the Sheet module
Private Sub Calendar1_DblClick()
'This will insert the ISO Weeknumber / Weekday in the cell
ActiveCell.Value = "W/D " & IsoWeekNum(Calendar1.Value) & " - " & _
Application.WorksheetFunction.Weekday(Calendar1.Value, 2)
ActiveCell.Select
End Sub
If you DblClick on the Calendar the ISO Week Number and Week Day will be placed
in the active cell. You also need the function below to use this.
Alt-F11
Insert>Module in the menu bar
paste the Function in this module
Alt-Q to go back to Excel
Public Function IsoWeekNum(d1 As Date) As Integer
' Daniel Maher
Dim d2 As Long
d2 = DateSerial(Year(d1 - Weekday(d1 - 1) + 4), 1, 3)
IsoWeekNum = Int((d1 - d2 + Weekday(d2) + 5) / 7)
End Function
More Information
See also this page if you want the Calendar control on a user form.
You can also Download the control on this page.
A Pop-up Calendar for Excel
http://www.fontstuff.com/vba/vbatut07.htm
For Excel 2007 see this Add-in
http://blogs.msdn.com/excel/archive/2007/08/01/sam-radakovitz-on-date-pickers.aspx
Use the Calendar control to fill in dates http://www.rondebruin.nl/calendar.htm
3 of 4 3/29/2010 12:39 AM
Use the Calendar control to fill in dates http://www.rondebruin.nl/calendar.htm
4 of 4 3/29/2010 12:39 AM

Вам также может понравиться