• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Need revised Error Handler if Dates not Entered|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Need revised Error Handler if Dates not Entered|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosNeed revised Error Handler if Dates…
sp_PrintTopic sp_TopicIcon
Need revised Error Handler if Dates not Entered
Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 71
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
1
July 9, 2022 - 8:01 am
sp_Permalink sp_Print

Hey everyone!  First, thanks for looking at my problem and being so willing to assist.  I have the code below that consists of 2 parts.  The first part is a date/time stamp with a user name in final 2 columns.  And the 2nd part is a requirement that columns T & U are only entered in date format.  This may seem trivial, but if you enter anything other than a date in T/U, the dialog box appears, and tells you it must be a date, however, the final 2 columns still record the date/time stamp and user name.  If for some reason the user does not enter the date at that time, I do not want the date/time stamp and user name for that row.  Only later when they come back and enter it.

 

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Row = 1 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
On Error GoTo ErrHandler
Application.EnableEvents = False
If Target.Column >= 12 Then ' <!-- Column L or higher is trigger
Range(Cells(Target.Row, 27), Cells(Target.Row, 27)).Value = Date + Time ' <!--- Need Date & Time
Range(Cells(Target.Row, 27), Cells(Target.Row, 27)).NumberFormat = "m/d/yyyy h:mm AM/PM" ' <!--- Format for Date/Timestamp
Range(Cells(Target.Row, 28), Cells(Target.Row, 28)) = Application.UserName ' <!--- Username
End If
ErrHandler:
Application.EnableEvents = True

Set w = ActiveSheet.Range("T:U") ' <!--- Columns that use dates & have data validations
For Each c In w
If c.Value <> "" And Not IsDate(c) Then
c.ClearContents
MsgBox "Only a date format is permitted in this cell."
End If
Next c

End Sub

sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOnlineSmall Online
2
July 10, 2022 - 6:26 pm
sp_Permalink sp_Print

Hi Sherry,

A few tips:

Range(Cells(Target.Row, 27), Cells(Target.Row, 27)).Value is not the right way, as both ranges point to the same cell. Just use Cells(Target.Row, 27).Value

I don't understand this line:

Set w = ActiveSheet.Range("T:U") ' <!--- Columns that use dates & have data validations

Basically, you check 2 full columns at every cell change... That's over 2 million cells to check at each cell change!

Check just current row, that's where you can use Range between 2 cells, more, you can do that check ONLY if the change is in T/U columns:

Set w = ActiveSheet.Range(Cells(Target.Row, "T"), Cells(Target.Row, "U"))

If Not Intersect(Target, w) is nothing then
If Target.Value <> "" And Not IsDate(Target) Then
Target.ClearContents
MsgBox "Only a date format is permitted in this cell."
End If
End If

 

Another one:
"If Target.Column >= 12 Then" will trigger the code even when a user makes a change in columns 27 or 28, where in fact the code is supposed to write. It makes sense to exclude these columns from triggering the code:

If Target.Column >= 12 Then should be:

Dim Col As Long: Col = Target.Column
If (Col >= 12 And Col < 20) Or (Col > 21 and Col<27) Then

All together, the code should look like this:

 

If Target.Row = 1 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False

Dim Col As Long: Col = Target.Column
If (Col >= 12 And Col < 20) Or (Col > 21 and Col<27) Then ' exclude columns T,U,27,28 or higher
Cells(Target.Row, 27).Value = Date + Time ' <!--- Need Date & Time
Cells(Target.Row, 27).NumberFormat = "m/d/yyyy h:mm AM/PM" ' <!--- Format for Date/Timestamp
Cells(Target.Row, 28).Value = Application.UserName ' <!--- Username
End If

Set w = ActiveSheet.Range(Cells(Target.Row, "T"), Cells(Target.Row, "U"))

If Not Intersect(Target, w) is nothing then
If Target.Value <> "" And Not IsDate(Target) Then
Target.ClearContents
MsgBox "Only a date format is permitted in this cell."
End If
End If

Application.EnableEvents = True

Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 71
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
3
July 13, 2022 - 5:44 am
sp_Permalink sp_Print sp_EditHistory

@Catalin Bombea,

 

That is quite helpful, and I understand about the 2 million cells, I am not sure how I overlooked that.  For this worksheet ("data") there are 501 rows of data.  This is used as a template, so although it may change it would only be once every few months when I prep the template with the new data data (via Power Query), and that can be changed as needed of course.

But there is one more thing, if a value that is NOT a date is entered into T/U (T2:U501), then the error dialog appears and says this muist be a date.  AND Columns AA/AB the Date/Time Stamp and User Name are not populate.  This IS correct.  However, if the user goes back an corrects column T/U with the date as required, then Columns AA/AB the Date/Time Stamp and User Name SHOULD be populated as it is the correct data.

I am sorry if I did not communicate that correctly.  But everything else is perfect.  Thanks so much!

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1810
Member Since:
November 8, 2013
sp_UserOnlineSmall Online
4
July 13, 2022 - 2:21 pm
sp_Permalink sp_Print sp_EditHistory

You can use this structure:

 

If Target.Row = 1 Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
Dim Col As Long: Col = Target.Column
If (Col >= 12 And Col < 20) Or (Col > 21 and Col<27) Then ' exclude columns T,U,27,28 or higher
FillTimeStamp Target
End If
Set w = ActiveSheet.Range(Cells(Target.Row, "T"), Cells(Target.Row, "U"))

If Not Intersect(Target, w) is nothing then
If Target.Value <> "" And Not IsDate(Target) Then
Target.ClearContents
MsgBox "Only a date format is permitted in this cell."

Else
FillTimeStamp Target
End If
End If

Application.EnableEvents = True

 

The procedure used in code above is:

Sub FillTimeStamp(Byval Target as Range)

Target.Parent.Cells(Target.Row, 27).Value = Date + Time ' <!--- Need Date & Time
Target.Parent.Cells(Target.Row, 27).NumberFormat = "m/d/yyyy h:mm AM/PM" ' <!--- Format for Date/Timestamp
Target.Parent.Cells(Target.Row, 28).Value = Application.UserName ' <!--- Username

End Sub

sp_AnswersTopicAnswer
Answers Post
Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 71
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
5
July 13, 2022 - 11:46 pm
sp_Permalink sp_Print

@Catalin Bombea,

PERFECTION!!!!  Thanks so very much!  I never would have figured this one out without your assistance!

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Catalin Bombea, Ramon Lagos
Guest(s) 6
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 205
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
stuart burge
Bruce Tang Nian
Scot C
Othman AL MUTAIRI
Misael Gutierrez Sr.
Attif Ihsan
Kieran Fee
Murat Hasanoglu
Brett Dryland
Saeed Aldousari
Forum Stats:
Groups: 3
Forums: 24
Topics: 6223
Posts: 27295

 

Member Stats:
Guest Posters: 49
Members: 31920
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.