+ Reply to Thread
Results 1 to 8 of 8

Extract Dates from String

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-23-2010
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    242

    Extract Dates from String

    I want to be able to extract dates (where excel recognizes it as a date) from a string.

    Here are some samples:

    From JAN-01-2011 To JAN-15-2011
    From JAN-01-2010

    it will always be in one of those two formats but the dates will change.

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Extract Dates from String

    Sub extract()
    
        Dim stra(2) As String
        Dim dte As Date
        
        stra(0) = "FROM JAN-01-2011 To JAN-15-2011"
        stra(1) = "FROM JAN-01-2010"
        
        For i = 0 To 1
            If Len(stra(i)) > 16 Then
                dte = CDate(Right(stra(i), 11))
                MsgBox dte
                dte = CDate(Right(Left(stra(0), 16), 11))
                MsgBox dte
            Else
                dte = CDate(Right(stra(i), 11))
                MsgBox dte
            End If
        Next i
    
    End Sub
    Here is one way to find the three dates.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Extract Dates from String

    sub snb()
      sq = Filter(Split("From JAN-01-2011 To JAN-15-2011"), "-")
      for j=0 to ubound(sq)
        x1 = CDate(sq(j))
      next
    End Sub



  4. #4
    Forum Contributor
    Join Date
    08-23-2010
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    242

    Re: Extract Dates from String

    Quote Originally Posted by snb View Post
    sub snb()
      sq = Filter(Split("From JAN-01-2011 To JAN-15-2011"), "-")
      for j=0 to ubound(sq)
        x1 = CDate(sq(j))
      next
    End Sub
    snb,

    That works great!! You are the man. I just need to do three other things with it:

    1) They need to be saved in two separate variables
    2) They need to be in date format
    3) It also needs to work if the sting only has one date in it (like the example I gave) - I think this one, I can just use part of davegugg's code but if you can think of a more efficient way, then I am all ears.

    btw, davegugg thank you for your help too. I appreciate everyone's help. And you gave me a few ideas as well.
    Last edited by djblois1; 07-15-2011 at 01:19 PM.

  5. #5
    Forum Contributor
    Join Date
    08-23-2010
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    242

    Re: Extract Dates from String

    Here is what I have so far:

    Sub snb()
    
    Dim j As Integer
    Dim sq As Variant
    Dim stra As Variant
    
        sq = Filter(Split("From JAN-01-2011 To JAN-15-2011"), "-")
     
        If Len(stra(i)) > 16 Then
            For j = 0 To UBound(sq)
                x1 = CDate(sq(j))
            Next
        Else
        
        End If
        
    End Sub
    I need to do 3 things with this code now:

    1) They need to be saved in two separate variables
    2) They need to be in date format
    3) It also needs to work if the sting only has one date in it (like the example I gave)

  6. #6
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Extract Dates from String

    The code I posted was already set up to do those three things:

    Sub extract()
    
        Dim stra(2) As String
        Dim dte1 As Date
        Dim dte2 As Date
        
        stra(0) = "FROM JAN-01-2011 To JAN-15-2011"
        stra(1) = "FROM JAN-01-2010"
        
        For i = 0 To 1
            If Len(stra(i)) > 16 Then
                dte1 = CDate(Right(stra(i), 11))
                dte2 = CDate(Right(Left(stra(0), 16), 11))
            Else
                dte1 = CDate(Right(stra(i), 11))
            End If
        Next i
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    08-23-2010
    Location
    Staten Island, NY
    MS-Off Ver
    Excel 2003
    Posts
    242

    Re: Extract Dates from String

    Thank you - it actually works great. Quick question:

    does this:

    Dim stra(2) As String
    mean that I can use a stra(0), Stra(1), and a Stra(2)?

    if so why didn't you use:

    Dim Dte(2) as Date
    Can I use that for any variable?

  8. #8
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Extract Dates from String

    It declares the variable as an array with three members. I really only needed two. You can easily find more info on vba arrays by doing a quick Google search. You could also set up the date variable as an array.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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