VBA for validating data

Godwin117

Board Regular
Joined
Dec 19, 2019
Messages
68
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have this excel document that dates are entered in cells F12 and G12 to the last row, data validation works when someone enters the data, but the dates don't change. I would like to know if there is a vba I could use to validate those dates and have a message box that shows where the errors are located.
 
I'm allowed myself to make two buttons instead of one. It's more convenient, i think. Paste this to ThisWorkbook module.
VBA Code:
Private Sub Workbook_Open()
Application.ScreenUpdating = False
On Error Resume Next

Dim iAF As Variant, iAG As Variant, iR&, lR&, d As Date: d = Date

lR = Cells(Rows.Count, 1).End(xlUp).Row
iAF = Range("F12:F" & lR).Value
    For iR = 1 To UBound(iAF)
            If iAF(iR, 1) > d Then
                Dim m As Integer, mp$, mt$
                mt = "Next?"
                mp = "Need to check Date in F" & iR& + 11
                    m = MsgBox(mp, 4 + 32, mt)
                        If m = 6 Then
                        Else
                            Exit Sub
                        End If
            End If
    Next
  
iAG = Range("G12:G" & lR).Value
    For iR = 1 To UBound(iAF)
            If iAG(iR, 1) < d Then
                mt = "Next?"
                mp = "Need to check Date in G" & iR& + 11
                    m = MsgBox(mp, 4 + 32, mt)
                        If m = 6 Then
                        Else
                            Exit Sub
                        End If
            End If
    Next

Application.ScreenUpdating = True
End Sub
This works great, Thank you, is there a way for it to skip if the cell is blank?
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
is there a way for it to skip if the cell is blank?
We need to replace the string
VBA Code:
If iAF(iR, 1)>d Then
with
Code:
If iAF(iR, 1)>d and not IsEmpty(iAF(iR, 1)) Then
and
VBA Code:
If iAG(iR, 1)>d Then
with
Code:
If iAG(iR, 1)>d and not IsEmpty(iAG(iR, 1)) Then
Try it (I typing from my phone, so could be wrong).
 
Upvote 0
@LazyBug
You reported some errors in the above post. To correct them, please just make another reply here, pointing out the corrections.
 
Upvote 0
Thanks for the answer, Peter_SSs.
I made a mistake in the second case.
The correct option will be
VBA Code:
If iAG(iR, 1) < d and not IsEmpty(iAG(iR, 1)) Then
instead of
VBA Code:
If iAG(iR, 1) < d Then
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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