+ Reply to Thread
Results 1 to 5 of 5

Converting Date:Time text to Date:Time value

Hybrid View

  1. #1
    Registered User
    Join Date
    10-10-2009
    Location
    BKK, Thailand
    MS-Off Ver
    Excel 2003
    Posts
    3

    Converting Date:Time text to Date:Time value

    Hello, I'm trying to write a VBA code for converting the date/time as text to a date/time value.

    The scenario is, I have three different types of text :
    ddmmmyy
    HH:MM
    ddmmmyy:HHMM

    I could have the code working fine with the first two types, but not the last one, please help me with this.

    Here is my code

    Private Sub StringToDateTime_Btn_Click()
    Dim c As Range
    Dim timeStr, timeStr2, timeStr3 As String
    
        For Each c In Selection
        On Error Resume Next
        Select Case True
    
        Case c.Text Like "##???##"
            timeStr = Format(c.Value, "ddmmmyy")
            c.NumberFormat = "[$-1010409]ddmmmyy;@"
            c.Value = timeStr
            
        Case c.Text Like "##:##"
            timeStr = TimeValue(c.Value)
            c.NumberFormat = "[$-1000000]hh:mm"
            c.Value = timeStr
       
        Case c.Text Like "##???##:####"
            
            'timeStr = Format(Mid(c.Value, 1, 7), "ddmmmyy")
            'timeStr2 = Mid(c.Value, 9, 2) & ":" & Mid(c.Value, 11, 2)
            'timeStr2 = Format(TimeValue(Format(timeStr2, "HH:MM")), "HHMM")
            'timeStr = TimeValue(timeStr & ":" & timeStr2)
            
            
            
           ' timeStr = Format(c.Value, "ddmmmmyy")
            'MsgBox Format(c.Value, "ddmmmmyy")
           ' c.NumberFormat = "[$-1010409]ddmmmyy:HHMM;@"
            'c.Value = timeStr
        
        End Select
        
    
            
    
        Next c
    End Sub
    Last edited by kacharuk; 10-13-2009 at 05:51 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Converting Date:Time text to Date:Time value

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    ie post a file which shows the values in place so we can better understand, test and validate both requirements and solution.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Converting Date:Time text to Date:Time value

    What is your Select case checking?

    Attach some example data
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    10-10-2009
    Location
    BKK, Thailand
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Converting Date:Time text to Date:Time value

    Hello, thank you for your response.
    Here is the example of what I expect my code to do.

    best regards,
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-10-2009
    Location
    BKK, Thailand
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Converting Date:Time text to Date:Time value

    Oh, I finally got it.
    The code is as follows, anyway if anyone knows a better way, your suggestion will be helpful for me . (I'm a novice, and don't know much about coding - - )

    Private Sub StringToDateTime_Btn_Click()
    Dim c As Range
    Dim timeStr, timeStr2, timeStr3 As String
    
        For Each c In Selection
        On Error Resume Next
        Select Case True
    
        Case c.Text Like "##???##"
            timeStr = Format(c.Value, "ddmmmyy")
            c.NumberFormat = "[$-1010409]ddmmmyy;@"
            c.Value = timeStr
            
        Case c.Text Like "##:##"
            timeStr = TimeValue(c.Value)
            c.NumberFormat = "[$-1000000]hh:mm"
            c.Value = timeStr
       
        Case c.Text Like "##???##:####"
            timeStr = Mid(c.Value, 1, 7) & "  " & Mid(c.Value, 9, 2) & ":" & Mid(c.Value, 11, 2)
            c.NumberFormat = "[$-1010409]ddmmmyy:HHMM;@"
            c.Value = timeStr
            
         
        
        End Select
    
        Next c
    End Sub

+ 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