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

Hello,

Im trying to play with this code.
i'm having problems with the second IF part.

thanks for your help in advance.

Private Sub Worksheet_Change(ByVal Target As Range)

pinput = Target.Value
' this changes the value from 330p to 3:30 PM
' this works
If Right(pinput, 1) = "p" And Len(pinput) 2 Then
newvalue = Left(Left(pinput, Len(pinput) - 1), Len(Left(pinput,
Len(pinput) - 1)) - 2) & ":" & Right(Left(pinput, Len(pinput) - 1), 2) & "
PM"
Target.Value = newvalue

Else
' this should for example change from 2p to 2:00 PM
' it does not work. it does nothing.

If Right(pinput, 1) = "p" And Len(pinput) < 2 Then
newvalue = Left(pinput, Len(pinput) - 1) & ":00" & " PM"
Target.Value = newvalue


End If
End If

End Sub


Thanks,


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default time question

Hi
change the lines
----
Else
' this should for example change from 2p to 2:00 PM
' it does not work. it does nothing.

If Right(pinput, 1) = "p" And Len(pinput) < 2 Then
newvalue = Left(pinput, Len(pinput) - 1) & ":00" & " PM"
Target.Value = newvalue
End If
----
to
----
ElseIf Right(pinput, 1) = "p" And Len(pinput) <= 2 Then
newvalue = Left(pinput, Len(pinput) - 1) & ":00" & " PM"
Target.Value = newvalue
---
Two changes:
- combination of Else and If statement
- Change <2 to <=2


--
Regards
Frank Kabel
Frankfurt, Germany

Cesar Zapata wrote:
Hello,

Im trying to play with this code.
i'm having problems with the second IF part.

thanks for your help in advance.

Private Sub Worksheet_Change(ByVal Target As Range)

pinput = Target.Value
' this changes the value from 330p to 3:30 PM
' this works
If Right(pinput, 1) = "p" And Len(pinput) 2 Then
newvalue = Left(Left(pinput, Len(pinput) - 1), Len(Left(pinput,
Len(pinput) - 1)) - 2) & ":" & Right(Left(pinput, Len(pinput) - 1),
2) & " PM"
Target.Value = newvalue

Else
' this should for example change from 2p to 2:00 PM
' it does not work. it does nothing.

If Right(pinput, 1) = "p" And Len(pinput) < 2 Then
newvalue = Left(pinput, Len(pinput) - 1) & ":00" & " PM"
Target.Value = newvalue


End If
End If

End Sub


Thanks,

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default time question

Hi Cesar,

Here's a shot

Private Sub Worksheet_Change(ByVal Target As Range)
Dim pinput, newvalue

Application.EnableEvents = False
On Error GoTo ws_exit
pinput = Target.Value
' this changes the value from 330p to 3:30 PM
' this works
If Right(pinput, 1) = "p" And Len(pinput) 3 Then
newvalue = Left(Left(pinput, Len(pinput) - 1), _
Len(Left(pinput, Len(pinput) - 1)) - 2) & ":" & _
Right(Left(pinput, Len(pinput) - 1), 2) & "PM "
Target.Value = newvalue
Else
' this should for example change from 2p to 2:00 PM
' it does not work. it does nothing.

If Right(pinput, 1) = "p" And Len(pinput) < 4 Then
newvalue = Left(pinput, Len(pinput) - 1) & ":00" & " PM"
Target.Value = newvalue
End If
End If

ws_exit:
Application.EnableEvents = True
End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Cesar Zapata" wrote in message
...
Hello,

Im trying to play with this code.
i'm having problems with the second IF part.

thanks for your help in advance.

Private Sub Worksheet_Change(ByVal Target As Range)

pinput = Target.Value
' this changes the value from 330p to 3:30 PM
' this works
If Right(pinput, 1) = "p" And Len(pinput) 2 Then
newvalue = Left(Left(pinput, Len(pinput) - 1), Len(Left(pinput,
Len(pinput) - 1)) - 2) & ":" & Right(Left(pinput, Len(pinput) - 1), 2) & "
PM"
Target.Value = newvalue

Else
' this should for example change from 2p to 2:00 PM
' it does not work. it does nothing.

If Right(pinput, 1) = "p" And Len(pinput) < 2 Then
newvalue = Left(pinput, Len(pinput) - 1) & ":00" & " PM"
Target.Value = newvalue


End If
End If

End Sub


Thanks,




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
Time Question metaltecks Excel Discussion (Misc queries) 2 March 12th 07 03:00 PM
Time Question colin Excel Discussion (Misc queries) 1 March 17th 06 12:37 PM
Time question Anthony Excel Discussion (Misc queries) 1 October 9th 05 05:15 PM
Time Question...is this possible? mileslit Excel Discussion (Misc queries) 1 September 8th 05 01:36 AM
Best time question Daniel Bonallack Excel Worksheet Functions 2 December 10th 04 11:37 PM


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

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

About Us

"It's about Microsoft Excel"