Resend my activation email : Register : Log in 
BCF: Bike Chat Forums


VBA for excel. Allow users to enter time with either . or :

Reply to topic
Bike Chat Forums Index -> The Geek Zone
View previous topic : View next topic  
Author Message

Mira
Scooby Slapper



Joined: 14 Feb 2009
Karma :

PostPosted: 14:19 - 08 Feb 2012    Post subject: VBA for excel. Allow users to enter time with either . or : Reply with quote

Hi folks,

I did a search on a excel forum and found some code that does what i want (mostly) which is to allow users to enter a time in either 7.3 or 7:30 and have it display as 7:30. Except it doesn't work if some one enters a time less than 1 for expamle enters 0.3 (meaning 0:30) it shows 7:12

Code:

Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "C4:V23"

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
If IsNumeric(.Value) And .Value >= 1 Then
.Value = (Int(.Value) + (.Value - Int(.Value)) * 100 / 60) / 24
.NumberFormat = "[h]:mm"
End If
End With
End If

ws_exit:
Application.EnableEvents = TrueEnd Sub

I have tried changing the Part of the code

If IsNumeric(.Value) And .Value >= 1 Then
to
If IsNumeric(.Value) And .Value >= 0 Then

That fixes the 0.3 problem but now if you try and enter 1:00 it changes it to 0:04 instead of leaving it as 1:00

Any sugestions?

Thanks, Mira. Confused
____________________
I have heard there are troubles of more than one kind. Some come from ahead and some come from behind. But I've bought a big bat.  I'm all ready you see.
Now my troubles are going to have troubles with me! ~ Dr Seuss.
 Back to top
View user's profile Send private message You must be logged in to rate posts

daemonoid
World Chat Champion



Joined: 27 Jun 2008
Karma :

PostPosted: 16:10 - 08 Feb 2012    Post subject: Reply with quote

not a vba programmer, but as a rough guess you should use this test instead:

if instr(1, .Value, ".") Then

instr returns the position of the . also so you can use that for the replacement. You'll have to tidy up the syntax yourself, like I said, don't do vba.
____________________
current: ducati monster 750
past: hyosung gt250r, bajaj pulsar 180, hyosung gt 125 comet
@thomasgarrard | www.straitjkt.com | www.racingseven.com
 Back to top
View user's profile Send private message Send e-mail Visit poster's website You must be logged in to rate posts

Mira
Scooby Slapper



Joined: 14 Feb 2009
Karma :

PostPosted: 23:30 - 08 Feb 2012    Post subject: Reply with quote

Thanks Thumbs Up
I'm not a VBA programmer either but I'll have a play with what you suggested and see what happens Shifty Laughing
____________________
I have heard there are troubles of more than one kind. Some come from ahead and some come from behind. But I've bought a big bat.  I'm all ready you see.
Now my troubles are going to have troubles with me! ~ Dr Seuss.
 Back to top
View user's profile Send private message You must be logged in to rate posts
Old Thread Alert!

The last post was made 14 years, 251 days ago. Instead of replying here, would creating a new thread be more useful?
  Display posts from previous:   
This page may contain affiliate links, which means we may earn a small commission if a visitor clicks through and makes a purchase. By clicking on an affiliate link, you accept that third-party cookies will be set.

Post new topic   Reply to topic    Bike Chat Forums Index -> The Geek Zone All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Read the Terms of Use! - Powered by phpBB © phpBB Group
 

Debug Mode: ON - Server: birks (www) - Page Generation Time: 0.07 Sec - Server Load: 1.64 - MySQL Queries: 13 - Page Size: 38 Kb