Put a Permanently Open Calendar on a Worksheet
One of my most popular tutorials shows you how to build A Pop-up Calendar for Excel. It involves building a VBA UserForm and placing an ActiveX Calendar Control on it. The user right-clicks on any cell and chooses InsertDate from the menu and the calendar appears. Choosing a date from the calendar places that date into the cell.But you can take a simpler route and have a calendar control embedded into the worksheet itself so that it is permanently visible. This requires very little code and is very easy to do. Here's how...
Display the Control Toolbox
Display the Control Toolbox by choosing View > Toolbars > Control Toolbox. Make sure that the Design Mode button (the first one on the Control Toolbox) is pressed in....Place a Calendar on the Worksheet
Click on the Calendar Control entry in the list then click on the worksheet approximately where you want to place the calendar. When you do this the calendar control will appear and you can move and resize it in the same way you would any drawing object...Write the VBA Code
Now you need to write the code that will make the calendar work. Right-click on the calendar and choose View Code from the context menu. This opens the Visual Basic Editor with the current worksheet's code module open and an event procedure ready for you to enter your code...
ActiveCell.Value = Calendar1.Value
The
code window should look like this:
Range("B3").Value = Calendar1.Value
When you have finished writing
your code you can close the Visual Basic Editor by clicking the
Close button in the top-right corner of the window or by
choosing File > Close and Return to Microsoft Excel.Activate the Calendar
The calendar will not work until you take Excel out of Design Mode. Click the Design Mode button on the Control Toolbox so that it is no longer depressed. The calendar is now ready to work.If at any time you want to make changes to the design of your calendar or the way it works simply display the Control Toolbox, place Excel in Design Mode and proceed as before.
Other Options
Synchronise the Calendar
You can have the calendar synchronise with the currently selected cell so that, if that cell contains a date, the calendar will show the same date. The code below uses a VBA If Statement to do this. If it does not find a date in the selected cell it makes the calendar display the current date.Return to the same same code window as before. At the top there are two drop-down lists. Open the left-hand list (it currently shows the name of the calendar) and choose Worksheet. The Visual Basic Editor should write an empty event procedure for the SelectionChange event. If it displays something else choose SelectionChange from the right-hand list. Then enter code as follows:
Private Sub
Worksheet_SelectionChange(ByVal Target As
Range)
If IsDate(ActiveCell.Value) Then
Calendar1.Value = ActiveCell.Value
Else
Calendar1.Value = Date
End If
End Sub
If IsDate(ActiveCell.Value) Then
Calendar1.Value = ActiveCell.Value
Else
Calendar1.Value = Date
End If
End Sub
Multiple Calendars
You can add as many calendars as you want. Each will have its own name and a separate event procedure and code (each referring to the relevant calendar by name).Keeping the Calendar on View
The easiest way to keep the calendar permanently on view when scrolling down the worksheet is to use Excel's Freeze Panes command. Select a cell in Column A a little further down the worksheet than the bottom of the calendar and choose Window > Freeze Panes. Now only the part of the worksheet below that cell will scroll.But if you want to get really clever with your calendar, you can have it move each time you select a different cell...
Private Sub
Worksheet_SelectionChange(ByVal Target As
Range)
Calendar1.Left = ActiveCell.Left + ActiveCell.Width
Calendar1.Top = ActiveCell.Top
End Sub
Calendar1.Left = ActiveCell.Left + ActiveCell.Width
Calendar1.Top = ActiveCell.Top
End Sub
Hiding the Calendar
You might want to hide the calendar when the selected cell doesn't contain a date (I have combined the code for hiding, moving and synchronising the calendar here)...
Private Sub
Worksheet_SelectionChange(ByVal Target As
Range)
If IsDate(ActiveCell.Value) Then
With Calendar1
.Visible = True
.Left = ActiveCell.Left + ActiveCell.Width
.Top = ActiveCell.Top
.Value = ActiveCell.Value
End With
Else
Calendar1.Visible = False
End If
End Sub
If IsDate(ActiveCell.Value) Then
With Calendar1
.Visible = True
.Left = ActiveCell.Left + ActiveCell.Width
.Top = ActiveCell.Top
.Value = ActiveCell.Value
End With
Else
Calendar1.Visible = False
End If
End Sub
No comments:
Post a Comment