1

Want to check if the cell is a date time value using the ISO format i.e. 2012-04-12T00:00:00

Current try:

If mainsht.Cells(r, 6).Value = "" Or mainsht.Cells(r, 6).Value = "####-##-##T##:##:## Then
GoTo next6

Still it does not seem to match the format in vba and cell value, as I have many cells with this correct format and still activating the else statement i.e. not recognized by the "####-##-##T##:##:##".

Maybe yyyy-mm-ddThh-MM-ss?

J.H
  • 181
  • 5
  • 14
  • 3
    When using wild cards you should use `Like`, not `=` – Tim Williams Jul 18 '16 at 04:38
  • Are you trying to tell whether there is a string in the cell which looks like a date, or are you trying to tell whether there is a number in the cell which has been formatted in that particular format? E.g. the current date/time (for me) is the number 42569.628827662 which can be formatted in many different ways, but is still a date. (And is still a number too.) – YowE3K Jul 18 '16 at 05:06
  • @TimWilliams Thanks, this is the answer I am looking for. – J.H Jul 21 '16 at 09:58
  • @YowE3K ISO Formatted date – J.H Jul 21 '16 at 09:58

4 Answers4

2

ISO date come in several formats, adding an asterisk "####-##-##T##:##:##*" would be more versatile.

2011-01-01T12:00:00Z
2011-01-01T12:00:00+05:00
2011-01-01T12:00:00-05:00
2011-01-01T12:00:00.05381+05:00

Example:

If mainsht.Cells(r, 6).Value = "" Or mainsht.Cells(r, 6).Value Like "####-##-##T##:##:##*" Then 

You might want to look at this post: Parsing an ISO8601 date/time (including TimeZone) in Excel

Community
  • 1
  • 1
0

The following UDF¹ can be used as a worksheet function or a helper function in a VBA project.

Option Explicit

Function IsISODateTime(str As String)
    Dim n As Long, nums() As Variant
    Static rgx As Object, cmat As Object

    'with rgx as static, it only has to be created once; beneficial with repeated calls to the UDF
    If rgx Is Nothing Then
        Set rgx = CreateObject("VBScript.RegExp")
    End If
    IsISODateTime = vbNullString

    With rgx
        .Global = False
        .MultiLine = False
        .Pattern = "[0-9]{4}\-[0-9]{2}\-[0-9]{2}[A-Z]{1}[0-9]{2}\:[0-9]{2}\:[0-9]{2}"
        IsISODateTime = .Test(str)
    End With
End Function

The UDF returns a true boolean True/False.

The pattern I've provided is very brick-by-brick literal; it could be shortened using the methods detailed in How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops.


¹ A User Defined Function (aka UDF) is placed into a standard module code sheet. Tap Alt+F11 and when the VBE opens, immediately use the pull-down menus to Insert ► Module (Alt+I,M). Paste the function code into the new module code sheet titled something like Book1 - Module1 (Code). Tap Alt+Q to return to your worksheet(s).

Community
  • 1
  • 1
0

As already pointed out by Tim Williams do it like so

If mainsht.Cells(r, 6).Value = "" Or mainsht.Cells(r, 6).Value Like "####-##-##T##:##:##" ...
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
0

To test the cell for an ISO formatted date you should check the cells NumberFormat property, so your If statement should be:

If mainsht.Cells(r, 6).Value = "" Or mainsht.Cells(r, 6).NumberFormat Like "yyyy-mm-ddThh:mm:ss*" Then

Note: If the currently accepted solution is working, your cell only contains a string value (which looks like an ISO formatted date) rather than an actual date displayed using an ISO date format.

YowE3K
  • 23,852
  • 7
  • 26
  • 40