+ Reply to Thread
Results 1 to 12 of 12

Thread: Extract date from string

  1. #1
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Extract date from string

    I have this string........actually its the contents of a cell..........I want to extract the date part of this string and then save it back onto itself.

    i am enclosing a file with the example strings.
    Attached Files Attached Files

  2. #2
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Extract date from string

    Maybe:

    Sub ExDate()
    Dim cell As Range
    For Each cell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        cell.Value = DateValue(Left(cell.Value, 11))
    Next 'cell
    End Sub

    Regards

  3. #3
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Extract date from string

    exactly what i was looking for.....thanks.

  4. #4
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,228

    Re: Extract date from string

    You're welcome. Thanks for the rep.

  5. #5
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Extract date from string

    Here is the code i ended up with. I combined code from several different threads i have posted...........i keep learning about how to more efficiently do things........this code is cool because it puts values into array efficiently and then extracts them from the array very efficiently.

    Dim ws As Worksheet
    Dim arrTime As Variant
    Dim arrIndex As Long
        
        
    Set ws = ActiveWorkbook.ActiveSheet
    
    'Assign to the array the entries from cell A2 to the last value in the column
    'very cool and fast way to put values into an array.
    arrTime = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
    
    
    For arrIndex = 1 To UBound(arrTime, 1)
    
        arrTime(arrIndex, 1) = DateValue(Left(arrTime(arrIndex, 1), 11))
    
    Next
    
    'This takes values from array and puts them into the sheet
    ws.Range("A2").Resize(UBound(arrTime, 1), 1).Value = arrTime


    Just thought i would share.

  6. #6
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Extract date from string

    one question i did have about the array that someone might answer is.........why is the array two dimensional........not sure why the creator of the code did it this way but it works.

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Extract date from string

    Interested in efficiency ?

    Sub snb()
      [A1:A100] = [if(A1:A100="","",datevalue(left(A1:A100,11)))]
    End Sub



  8. #8
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Extract date from string

    ok, that is just too cool. but what about if the number of rows in col A is variable and could be more than 100?

    very cool !

  9. #9
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Extract date from string

    Either you take a safe range:

    Sub snb()
      [A1:A10000] = [if(A1:A10000="","",datevalue(left(A1:A10000,11)))]
    End Sub
    or you use a dynamically named range:

    Sub snb_001()
      columns(1).specialcells(2).name="c00"
      [c00] = [if(c00="","",datevalue(left(c00,11)))]
    End Sub



  10. #10
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Extract date from string

    thanks! Had a few questions.

    1) What is c00?

    2) what is this doing exactly?

    columns(1).specialcells(2).name="c00"

  11. #11
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Extract date from string

    "c00" is a text string; any string would do as long as you use it in the next line in the places where c00 is now.

    second question: that line makes a named range of all the cells in column A that contain values. The name of that range is "c00" in this example.



  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: Extract date from string

    Quote Originally Posted by welchs101 View Post
    one question i did have about the array that someone might answer is.........why is the array two dimensional........not sure why the creator of the code did it this way but it works.
    If you assign a range to a Variant, you always get a 2D array, even if the range is only one row or column.

+ 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.2.0