Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Convert values to time: Error Trapping


I am trying to write a macro that the user can enter time as integers
ie:
100=1:00
30=:30
123=1:23
I have this code, and am wishing to add to the error trapping so that
the target does not = :99 if the user enters 99
or :72 if 72 is entered
Any ideas?

Code:
--------------------
Private Sub Worksheet_Change(ByVal Target As Range)
Dim t As Integer
Dim i As String, ni As String
t = Target.Column
If t < 3 Then
i = Target.Value
On Error GoTo error
If i 1 And i <= 2400 Then
ni = Left(i, Len(i) - 2) & ":" & Right(i, 2)
Application.EnableEvents = False
Target = ni
Application.EnableEvents = True
Else: MsgBox "Must have more than 1 number and not be greater than 2400"
Target.Select
Exit Sub
error: MsgBox "Must be a number"
Target.Select
End If
End If

End Sub

--------------------


Thanks


--
Stringer
------------------------------------------------------------------------
Stringer's Profile: http://www.thecodecage.com/forumz/member.php?userid=117
View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=64692

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 222
Default Convert values to time: Error Trapping

You're parsing the numbers, but not INTO a time format. In a worksheet
function, I would do the same thing you're doing, but like this:

=TIME(0,LEFT(A2,1),RIGHT(A2,2))

So, do the same thing in your macro, make sure the minutes and seconds are
slotting into MINUTE portion of a time function, and the same for SECONDS. By
doing this, you get the following results:

123 = 0:01:23
199 = 0:02:39

--
"Actually, I *am* a rocket scientist." -- JB

Your feedback is appreciated, click YES if this post helped you.


"Stringer" wrote:


I am trying to write a macro that the user can enter time as integers
ie:
100=1:00
30=:30
123=1:23
I have this code, and am wishing to add to the error trapping so that
the target does not = :99 if the user enters 99
or :72 if 72 is entered
Any ideas?

Code:
--------------------
Private Sub Worksheet_Change(ByVal Target As Range)
Dim t As Integer
Dim i As String, ni As String
t = Target.Column
If t < 3 Then
i = Target.Value
On Error GoTo error
If i 1 And i <= 2400 Then
ni = Left(i, Len(i) - 2) & ":" & Right(i, 2)
Application.EnableEvents = False
Target = ni
Application.EnableEvents = True
Else: MsgBox "Must have more than 1 number and not be greater than 2400"
Target.Select
Exit Sub
error: MsgBox "Must be a number"
Target.Select
End If
End If

End Sub

--------------------


Thanks


--
Stringer
------------------------------------------------------------------------
Stringer's Profile: http://www.thecodecage.com/forumz/member.php?userid=117
View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=64692


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Convert values to time: Error Trapping

How about something like this instead...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewTime As Variant
If Target.Column < 3 Then
NewTime = Format(Target.Value, "00:00")
If IsDate(NewTime) Then
Application.EnableEvents = False
Target.NumberFormat = "hh:mm"
Target.Value = CDate(NewTime)
Application.EnableEvents = True
Else
Target.NumberFormat = "General"
Target.Select
MsgBox "I am not able to convert that entry into a time value!"
End If
End If
End Sub

--
Rick (MVP - Excel)


"Stringer" wrote in message
...

I am trying to write a macro that the user can enter time as integers
ie:
100=1:00
30=:30
123=1:23
I have this code, and am wishing to add to the error trapping so that
the target does not = :99 if the user enters 99
or :72 if 72 is entered
Any ideas?

Code:
--------------------
Private Sub Worksheet_Change(ByVal Target As Range)
Dim t As Integer
Dim i As String, ni As String
t = Target.Column
If t < 3 Then
i = Target.Value
On Error GoTo error
If i 1 And i <= 2400 Then
ni = Left(i, Len(i) - 2) & ":" & Right(i, 2)
Application.EnableEvents = False
Target = ni
Application.EnableEvents = True
Else: MsgBox "Must have more than 1 number and not be greater than 2400"
Target.Select
Exit Sub
error: MsgBox "Must be a number"
Target.Select
End If
End If

End Sub

--------------------


Thanks


--
Stringer
------------------------------------------------------------------------
Stringer's Profile:
http://www.thecodecage.com/forumz/member.php?userid=117
View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=64692


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Convert values to time: Error Trapping


Thanks Rick, that works very well.


--
Stringer
------------------------------------------------------------------------
Stringer's Profile: http://www.thecodecage.com/forumz/member.php?userid=117
View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=64692

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error trapping? IanC[_2_] Excel Programming 2 June 4th 08 11:55 AM
while deleting rows it finds an error - error trapping Janis Excel Programming 2 July 19th 07 12:12 AM
error trapping [email protected] Excel Programming 2 January 20th 05 10:07 PM
Error trapping Luis Excel Programming 1 November 11th 04 03:25 AM
Error Trapping Neil Excel Programming 1 January 5th 04 04:38 PM


All times are GMT +1. The time now is 02:35 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"