Help with VBA code

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I want to have a message box pop up if the date entered in column A of a table row is earlier than todays date, I have some code but it only seems to sometimes work. What is wrong with it or is there better code? The table's name is npss_quote

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ans As Long
      If Not Intersect(Target, Range("A:A")) Is Nothing Then
    Application.EnableEvents = False
        If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
        If Target.Value < Date Then
            ans = MsgBox("This input is older than today !....Are you sure that is what you want ???", vbYesNo)
        If ans = vbNo Then Target.Value = ""
    End If
End If
Application.EnableEvents = True

End Sub
 
So is that what you want? If not explain what you do want.
I want a message box to appear if a date entered is earlier then today's date, asking for confirmation. But for it to occur with every early date that is entered.
 
Last edited:
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Well you posted a script you found.

The script you found activates when you enter a date in column A
It checks that particular date you just entered.
It does not check all the dates previously entered.
 
Upvote 0
try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ans As Long, rng As Range
Set rng = ActiveSheet.ListObjects("npss_quote").ListColumns(1).Range
If Not Intersect(Target, rng) Is Nothing Then
    If Target.Count <> 1 Or IsEmpty(Target) Then Exit Sub
    If Target.Value < Date Then
        ans = MsgBox("This input is older than today !....Are you sure that is what you want ???", vbYesNo)
        If ans = vbNo Then
            Application.EnableEvents = False
            Target.Value = ""
            Application.EnableEvents = True
        End If
    End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,424
Messages
6,124,817
Members
449,190
Latest member
rscraig11

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