PDA

View Full Version : Solved: Setting default date in calendar



antonf
11-03-2009, 10:41 AM
Hi guys

I use the code below in columns A and B to enter dates, always in pairs in the same row.

The date in column A is always entered first. It works fine with the default date being today's date. What I would however like is when I select the adjacent cell in column B that the default date is the day after the date in column A.

Thanks a lot!


Option Explicit

Private Sub Calendar1_Click()
ActiveCell.Value = CDbl(Calendar1.Value)
ActiveCell.NumberFormat = "dd-mmm-yy"
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("A2:B900"), 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
If Not IsDate(Target.Value) Then
Calendar1.Value = Date
Else
Calendar1.Value = Target.Value
End If
ElseIf Calendar1.Visible Then Calendar1.Visible = False
End If
End Sub

xld
11-03-2009, 10:45 AM
Option Explicit

Private Sub Calendar1_Click()
ActiveCell.Value = CDbl(calendar1.Value)
ActiveCell.NumberFormat = "dd-mmm-yy"
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("A2:B900"), 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
If Not IsDate(Target.Value) Then

If Target.Column = 1 Then

calendar1.Value = Date
Else
calendar1.Value = Target.Offset(0, -1).Value + 1
End If
Else

calendar1.Value = Target.Value
End If
ElseIf calendar1.Visible Then calendar1.Visible = False
End If
End Sub

antonf
11-03-2009, 11:04 AM
Darn, that was QUICK!!!

Now I need to find a calendar that has buttons to jump months/years, but this is great!

lucas
11-03-2009, 11:29 AM
Are you going to be using this on only one computer or will you be sharing it with others?

calendar control has a problem with portability.

antonf
11-03-2009, 11:33 AM
Only on my own computer...