+ Reply to Thread
Results 1 to 4 of 4

Checking for a Date in a Cell

Hybrid View

  1. #1
    Registered User
    Join Date
    12-20-2012
    Location
    Dallas, TX
    MS-Off Ver
    Excel2010
    Posts
    21

    Checking for a Date in a Cell

    I am using the following code to determine if a cell in a range contains a date, and if so to act by adding 100 years to the date:

    For Each c In Range("A88:A130")
    If IsDate(c.Value) And DatePart("yyyy", c.Value) < "2000" Then c.Value = DateAdd("yyyy", 100, c.Value)
    Next c

    It seems to work fine if the cells have a date or are empty, but it crashes if any of the cells contain anything other than a date, such as spaces or text. How can I fix this? Any help will be greatly appreciated. Thanks.

  2. #2
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Checking for a Date in a Cell

    You could add a line to check if c is numeric.

    If isNumeric(c) then
    do your code
    Else: End if

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Checking for a Date in a Cell

    Check for a date, and then do the rest of the checking etc. only if there is a date.
    For Each c In Range("A88:A130")
        If IsDate(c.Value) Then
            If Year(c.Value) < 2000 Then 
                c.Value = DateAdd("yyyy", 100, c.Value)
            End If
        End If
    Next c
    Last edited by Norie; 05-18-2015 at 01:25 PM.
    If posting code please use code tags, see here.

  4. #4
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Checking for a Date in a Cell

    I didn't know there was an IsDate function. Thank you Norie.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] checking if a date/date range already exists in 4 other date storing tables
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-13-2014, 06:01 PM
  2. Checking if date value in cell is between or equal to start and end date
    By Beckmonkey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-23-2014, 02:20 PM
  3. [SOLVED] Checking to see if a date is within current month from a column of date values using vba
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-20-2013, 06:51 PM
  4. Replies: 4
    Last Post: 01-31-2012, 04:07 PM
  5. Put todays date in a cell by checking a box!!
    By Martin in forum Excel General
    Replies: 1
    Last Post: 10-24-2005, 04:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1