Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Business Applications
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Business Applications >
Solved: Excel Macro -> Time

Reply  
Thread Tools
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
17-Nov-2009, 10:35 AM #1
Question Solved: Excel Macro -> Time
I am trying to figure out the best way to copy a value from one cell, and paste it into another cell as a time value.

The cell to be copied can have values such as:

1152
930
450
0823

What I want to do is place a colon between the first two numbers and the last two numbers. I have code that does this but it fails when there is not a leading zero.

How would I do this be determining if there is a leading zero or not?
__________________
Computerman29642
"Why so serious?....let's put a smile on that face" - The Joker
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
17-Nov-2009, 10:45 AM #2
Here is the solution I have come up with...

Code:
If Len(wsTest.Range("L6")) = 4 Then
                wsTest1.Cells(x, "E") = Left(wsTest.Range("L6"), 2) _
                & ":" & Right(wsTest.Range("L6"), 2)
            Else
                wsTest1.Cells(x, "E") = Left(wsTest.Range("L6"), 1) _
                & ":" & Right(wsTest.Range("L6"), 2)
            End If
Zack Barresse's Avatar
Computer Specs
Distinguished Member with 5,030 posts.
 
Join Date: Jul 2004
Location: Oregon, United States
Experience: I'ma learnin'!
17-Nov-2009, 07:02 PM #3
You could do it with one line. I added the test for a date so you wouldn't duplicate the action, but just do it based on the length from the right minus 2. There is no check for overall length or if it is a number, or even if the cell has a value, so lots of errors to think about...
Code:
    If IsDate(wsTest.Range("L6").Value) = False Then
        wsTest.Range("L6").Value = Left(wsTest.Range("L6").Value, Len(wsTest.Range("L6").Value) - 2) & ":" & Right(wsTest.Range("L6").Value, 2)
    End If
Edit: Here is a simpler looking code by assigning the value to a variable (looks easier to understand a little shortened up)...

Code:
Sub testAddColon()

    Dim wsTest As Worksheet, iColon As Long, vVal As Variant
    
    Set wsTest = ActiveSheet
    vVal = wsTest.Range("L6").Value
    
    If IsDate(vVal) = False Then
        vVal = Left(vVal, Len(vVal) - 2) & ":" & Right(vVal, 2)
    End If

End Sub
HTH
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
18-Nov-2009, 08:54 AM #4
Thanks Zack.
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:27 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.