Results 1 to 5 of 5

Thread: Convert String to date via Code

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    61

    Convert String to date via Code

    I have a large sheet with several columns that list date time like this

    0820 (5/3)

    I'd like to search the entire sheet and anytime a cell has a date with this format convert it to
    05032010 :0820

    Anyone have coding they can share with me?

    Thanks
    Steve

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Convert String to date via Code

    If I were to look at 0820 (5/3) I would not know that is a date. How would your code know?

    If all these numbers that are actually dates were in the same column, and nothing else was in that column, the program could assume that whatever it finds is a date and format it accordingly.

    Are these all in the same column or spread willy nilly all over the sheet?

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    61

    Re: Convert String to date via Code

    Quote Originally Posted by Hack View Post
    If I were to look at 0820 (5/3) I would not know that is a date. How would your code know?

    If all these numbers that are actually dates were in the same column, and nothing else was in that column, the program could assume that whatever it finds is a date and format it accordingly.

    Are these all in the same column or spread willy nilly all over the sheet?
    They are all in the same column.

    The column heading helped me to determine that it was a time.

    I thought I could use Mid() but I don't know how to handle single vs double digits like
    0820 (5/3) or 0820 (10/11)

  4. #4
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Convert String to date via Code

    I thought I could use Mid() but I don't know how to handle single vs double digits like
    0820 (5/3) or 0820 (10/11)
    The simple/quick answer to that would be to determine the length, which would also have to assume that the time is always 4 digits...
    Code:
    Dim date1 as string
    Dim date2 as string
    date1 = "0820 (10/11)"
    date2 = "0820 (5/11)"
    
    Len(date1) 'this would return 12
    Len(date1) 'this would return 11
    so by determining the length you can determine which portion you need to extract.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Convert String to date via Code

    are your dates (dd/mm) or (mm/dd)? as both are ambiguous it is hard to tell

    though i guess it does not matter, try like
    vb Code:
    1. mystr = "0820 (5/3)"
    2. datestart = InStr(mystr, "(") + 1
    3. sep = InStr(mystr, "/") + 1
    4. datend = Len(mystr) - 1
    5. mydate = Format(Mid(mystr, datestart, sep - datestart - 1), "00") & _
    6. Format(Mid(mystr, sep, datend - sep + 1), "00") & Year(Now) & " :" & Left(mystr, 4)
    7. msgbox mydate
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width