DoubleClick event to enter date

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a spreadsheet where column H:I will receive a date. All of this is done thru a DoubleClick event with calls the calendar function. All of this works well. Now I need to ensure the user does not simply enter something into the cell that is not a date. I've added a SheetChange event and it seems to be working well, but just curios if this is the proper way to do it? This DoubleClick event is happening on multiple sheets therefore in the ThisWorkbook module.

VBA Code:
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Cancel = True
If Target.CountLarge > 1 Or Target.Row < 3 Then Exit Sub
If Sh.Range("A1").Value = "Unit" Then
If Not Intersect(Target, Sh.Range("H:I")) Is Nothing Then
Call AskFor_A_Date
End If
End If
End Sub

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.CountLarge > 1 Or Target.Row < 3 Then Exit Sub
Dim msg As String: msg = "Please double click in the cell and use the calendar to enter a date"
If Sh.Range("A1").Value = "Unit" Then
If Not Intersect(Target, Sh.Range("H:I")) Is Nothing Then
If Not IsNumeric(Target.Value) Then
Application.EnableEvents = False
Target.ClearContents
CreateObject("WScript.Shell").Popup msg, 1, "Enter Date"
Target.Select
Application.EnableEvents = True
End If
End If
End If
End Sub
 
Ok, I think this will do it.

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.CountLarge > 1 Or Target.Row < 3 Then Exit Sub
    Dim oldDate As Date: oldDate = Date - 365
    Dim lateDate As Date: lateDate = Date + 365
    Dim msg As String: msg = "Please double click in the cell and use the calendar to enter a date"
    If Sh.Range("A1").Value = "Unit" Then
        If Not Intersect(Target, Sh.Range("H:I")) Is Nothing Then
            If Target.Value < Int(CDbl(oldDate)) Or Target.Value > Int(CDbl(lateDate)) Or Not IsNumeric(Target.Value) Then
                Application.EnableEvents = False
                Target.Select
                Call AskFor_A_Date
                Application.EnableEvents = True
            End If
        End If
    End If
End Sub
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi Fluff,

How about if I just check the manually entered number to make sure it falls between today -/+ 365?
I don't think this will necessarily work exactly as you want because of possible incorrect data conversions, say at future date and/or by user (intentionally or not)

@Fluff earlier suggestion of locking the columns from user-entry just removes the whole mechanism for the user to even enter data into the cell so you can avoid extra processing or code needed to check/test data entry.

Locking the columns doesn't mean the user can't select the cell, so they can and infact are forced to double-click which triggers your code - without adding anything new to do the testing/checking part!

Ignore - just seen your post and the CDbl for conversion, that should cover any incorrect data inputs!
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top