Help with VBA code

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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