+ Reply to Thread
Results 1 to 4 of 4

Converting Unformatted string into Time Value (VBA; error on one line of code)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2013
    Location
    Los angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    109

    Converting Unformatted string into Time Value (VBA; error on one line of code)

    I'm attempting to convert a column of data (exported from a database so its unformatted) into a time format recognizable by Excel.
    i.e. I want to change "804a" to "8:04 AM" or "645p" to "6:45 PM"

    I'm receiving a run-time error "Application-defined or object-defined error" on this line:
    cell.Formula = "=Time(Left(" & cell.Value & ", 1), Left(Right(" & cell.Value & ", 3), 2), 0)"
    Here is the subroutine in its entirety:

    Sub colon()
    
    Dim lastcolumn As Integer
    Dim lastrow As Integer
    Dim cell As Range
    
    
    
    
    'Find last column of data
    With ActiveWorkbook.ActiveSheet.Range("A1")
        lastcolumn = Cells(2, Columns.Count).End(xlToLeft).Column
        lastrow = Cells(Rows.Count, lastcolumn).End(xlUp).Row
    
        Range(.Offset(1, lastcolumn - 2), .Offset(lastrow, lastcolumn - 2)).Name = "combined"
        
    End With
    
    
    'Convert simple time into formatted time
    For Each cell In Range("combined")
    
        If Len(cell) = 4 Then
    
            cell.Formula = "=Time(Left(" & cell.Value & ", 1), Left(Right(" & cell.Value & ", 3), 2), 0)"
        End If
    Next
               
        
    End Sub
    Any suggestions or help is greatly appreciated.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Converting Unformatted string into Time Value (VBA; error on one line of code)

    Hello aloha31,

    Welcome to the Forum!

    Sub colon()
    
    Dim lastcolumn As Integer
    Dim lastrow As Integer
    Dim cell As Range
    Dim n As Long
    
    
    'Find last column of data
    With ActiveWorkbook.ActiveSheet.Range("A1")
        lastcolumn = Cells(2, Columns.Count).End(xlToLeft).Column
        lastrow = Cells(Rows.Count, lastcolumn).End(xlUp).Row
    
        Range(.Offset(1, lastcolumn - 2), .Offset(lastrow, lastcolumn - 2)).Name = "combined"
        
    End With
    
    
    'Convert simple time into formatted time
    For Each cell In Range("combined")
    
        n = Len(cell)
        If n = 4 Or n = 5 Then
            cell = Left(cell, n - 3) & ":" & Right(cell, 3)
            cell.NumberFormat = "h:mm AM/PM"
        End If
    Next
                
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    07-12-2013
    Location
    Los angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    109

    Re: Converting Unformatted string into Time Value (VBA; error on one line of code)

    Thanks so much! Works great. One additional question: Is there a way to undo the effects of a macro once you run it (the undo button becomes grayed out after running the subroutine)?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Converting Unformatted string into Time Value (VBA; error on one line of code)

    Hello alhoa31,

    With a VBA macro, there is no Undo feature.

+ 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