+ Reply to Thread
Results 1 to 9 of 9

Convert string dates to proper numerical date format

Hybrid View

  1. #1
    Registered User
    Join Date
    07-17-2008
    Location
    us
    Posts
    14

    Convert string dates to proper numerical date format

    Hello, Everyone!

    I am trying to convert text 'dates' like Sat. 1/05/2007 to real dates. I need to find all 'date' cells in range B4:B100 (they are filled in yellow), strip off the offending text (i.e. Sat.), and use DateValue to turn the rest into a proper, numerical date in the ddd mm/dd/yy format, arial, bold, 10pt, and centered in place.

    I've spent days trying to write this macro. I've had help from experts in forums, but whenever I need to make any slight modification, a
    trainwreck ensues. Please see the code below.

    Sub switchDate()
    Dim MyDate As String
    For Each cell In ActiveSheet.Range("B4:B100")
        If cell.Interior.ColorIndex <> xlNone Then
           cell.Value = MyDate.Value
                MyDate = Trim(MyDate)
                If InStr(MyDate, " ") > 0 Then
                MyDate = Mid(MyDate, InStr(MyDate, " ") + 1)
                End If
            MyDate = DateValue(MyDate)
        End If
    Next
    End Sub


    This doesn't even begin to work and I can't begin to guess why. Could
    somebody out there please make the tweaks and tell me what I did wrong, that I might not do it hence?

    I thank you greatly, and have a terrific day.

    Arlen
    Last edited by VBA Noob; 07-21-2008 at 01:06 PM.

  2. #2
    Registered User
    Join Date
    01-18-2005
    Location
    Australia
    Posts
    29
    Hi Arien

    I have altered your code and provided some comments (you should be able to compare the code below with your code to see where you went wrong):

    Sub switchDate()
        Dim MyDate As String 'Declare your vaiable
        
        For Each cell In ActiveSheet.Range("B4:B100") 'Set range for your loop
            
            If cell.Interior.ColorIndex <> xlNone Then '(first if)If  the cell contains a colour do the next statment
                MyDate = cell.Value 'pass the value of the cell to the variable
                MyDate = Trim(MyDate) 'Remove any leading or trailing spaces from the variables value
                
                If InStr(MyDate, " ") > 0 Then '(second if) If there are any spaces in the variables value do the next statement ie. proceed
                    MyDate = Mid(MyDate, InStr(MyDate, " ") + 1) 'Get anything after a space and pass it back to the variable
                End If ' End of second if statement
                MyDate = DateValue(MyDate) 'Convert variable value to a date
            
                cell.Value = MyDate 'Write variables value back to the cell from the information was collected
            End If ' End of first if statement
        Next ' loop
    End Sub
    Cheers
    Shaun
    Last edited by ShaunM; 07-21-2008 at 01:59 AM.

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Arlen

    Can you please take time to review the forum rules, specifically the rules regarding searchable titles, and code wrapping.

    I've edited your post this time for you.

    rylo

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,663
    1. The rules request that you wrap your code...
    2. (a) The variable MyDate is declared as string-type; but in line #5 you wrote: MyDate.Value, which should have given you a compile error because strings have no .Value qualifier.
    (b) Line #5 is trying to write the contents of the variable to the cell:
    (Let the) cell.Value = MyDate.Value
    3. Since your For..Each loop is testing the formatting of a cell, line #5 is meant to load the variable MyDate with the contents of the cell. It should be:
    MyDate = Cell.Value
    Note: MyDate is empty (you did not initialize it prior to line #5) so that the code:
    cell.Value = MyDate
    would have erased the contents of the test cell!
    Ben Van Johnson

  5. #5
    Registered User
    Join Date
    07-17-2008
    Location
    us
    Posts
    14
    Shaun and Proton,

    The code is perfect. You're both awesome for taking the time to explain this. This is the only way I can really learn VBA at all.

    Now, I've rounded up this final bit of code to format the dates after converting so that they look like they did as text...Arial, 10, bold and centered. Do I use cell As Range, or create a new Range, r? And where does it all go...

                 Dim r as Range (is up top)
    
    and this goes...
                   With r
                    .NumberFormat = "ddd mm/dd/yy"
                    With .Font
                        .Bold = True
                        .Name = "arial"
                        .Size = 10
                    End With

    And, truly, that is all.

    Thanks guys.

  6. #6
    Registered User
    Join Date
    01-18-2005
    Location
    Australia
    Posts
    29
    Hi Arlen

    I have modified your code to inculde your formatting statements:

    Sub switchDate()
        Dim MyDate As String 'Declare your vaiable
        
        For Each cell In ActiveSheet.Range("B4:B100") 'Set range for your loop
            
            If cell.Interior.ColorIndex <> xlNone Then '(first if)If  the cell contains a colour do the next statment
                MyDate = cell.Value 'pass the value of the cell to the variable
                MyDate = Trim(MyDate) 'Remove any leading or trailing spaces from the variables value
                
                If InStr(MyDate, " ") > 0 Then '(second if) If there are any spaces in the variables value do the next statement ie. proceed
                    MyDate = Mid(MyDate, InStr(MyDate, " ") + 1) 'Get anything after a space and pass it back to the variable
                End If ' End of second if statement
                MyDate = DateValue(MyDate) 'Convert variable value to a date
            
                cell.Value = MyDate 'Write variables value back to the cell from the information was collected
                
                cell.NumberFormat = "ddd mm/dd/yy"
                
                With cell.Font
                    .Bold = True
                    .Name = "arial"
                    .Size = 10
                End With
    
            
            End If ' End of first if statement
        Next ' loop
    Your were very close all you needed to do was supply the sub with the reference to what you wanted formated. (there was no need to redeclare the range)

    Cheers

    Shaun

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666
    Quote Originally Posted by Codpops
    Shaun and Proton,

    The code is perfect. You're both awesome for taking the time to explain this. This is the only way I can really learn VBA at all.

    Now, I've rounded up this final bit of code to format the dates after converting so that they look like they did as text...Arial, 10, bold and centered. Do I use cell As Range, or create a new Range, r? And where does it all go...

                 Dim r as Range (is up top)
    
    and this goes...
                   With r
                    .NumberFormat = "ddd mm/dd/yy"
                    With .Font
                        .Bold = True
                        .Name = "arial"
                        .Size = 10
                    End With

    And, truly, that is all.

    Thanks guys.
    You [Offensive comment removed by Forum Administrator].
    That's part of my code in your CROSS POST (MrExcel)
    Last edited by Paul; 07-21-2008 at 01:58 PM.

  8. #8
    Registered User
    Join Date
    07-17-2008
    Location
    us
    Posts
    14
    Jindon,

    I replied to you in the other forum.

    I said thank you for helping me. You provided 1/2 of the puzzle. In this forum, I said I 'rounded up' the code, so I could get help figuring out the other half. I didn't say I created it.

    I'm now aware that most of the experts show up on multiple forums, so next time I will give you props and I'm sure they'll know who you are.

    Shaun,

    I haven't tried the code yet, but again I thank you, especially for the explanation, because without that, I'm just typing but not necessarily knowing why.

    Have a great day, both of you.

    Arlen

  9. #9
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Codpops,

    While we don't condone cross-posting, we understand users are going to do it. Please review the rules, and if you plan to cross-post in the future be sure to post a link to the other post.

    Also, if you get a resolution to your problem on the other site be sure to include that in this forum.

    Thank you,
    Paul

+ 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