Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
Lee Hunter
 
Posts: n/a
Default Input mask for active cell

I am attempting to set up an input mask for the active cell such that the
time can be entered as 3 or 4 digits without the full colon.(815 for 08:15
AM)

In the sample code below, I never get to the numberformat property and the
code sems to be recursively called from itself. I've tried both the
Calculate and the Change events. What's my best solution?
Thanks!
Lee

Private Sub Worksheet_Some_event(ByVal Target As Range
Dim InTime As String
Dim Apostrophe As String
Apostrophe = ""
If Len(Target) = 4 Then ' Allow 815 instead of 0815
InTime = Apostrophe & Left(Target.Value, 2) & ":" & Right(Target.Value, 2) &
Apostrophe
Else: InTime = Apostrophe & "0" & Left(Target.Value, 1) & ":" &
Right(Target.Value, 2) & Apostrophe
End If
ActiveCell.Value = InTime
ActiveCell.NumberFormat = "hh:mm A"
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Input mask for active cell

First, you can't make up your own events. But you can tie into the
worksheet_Change event.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count 1 Then Exit Sub
If Intersect(Target, Me.Range("C:C")) Is Nothing Then Exit Sub

On Error GoTo errHandler:

If IsNumeric(Target.Value) = False Then Exit Sub
If Int(Target.Value) < Target.Value Then Exit Sub
If Target.Value < 0 Then Exit Sub

If Target.Value < 9999 Then
Application.EnableEvents = False
Target.Value = Format(Target.Value, "00:00")
Target.NumberFormat = "hh:mm AM/PM"
End If

errHandler:
Application.EnableEvents = True

End Sub

Notice the application.enableevents stuff. That's what stops the change to the
worksheet from calling the worksheet_change event.

You may want to take a look at how Chip Pearson approached the subject:
http://cpearson.com/excel/DateTimeEntry.htm






Lee Hunter wrote:

I am attempting to set up an input mask for the active cell such that the
time can be entered as 3 or 4 digits without the full colon.(815 for 08:15
AM)

In the sample code below, I never get to the numberformat property and the
code sems to be recursively called from itself. I've tried both the
Calculate and the Change events. What's my best solution?
Thanks!
Lee

Private Sub Worksheet_Some_event(ByVal Target As Range
Dim InTime As String
Dim Apostrophe As String
Apostrophe = ""
If Len(Target) = 4 Then ' Allow 815 instead of 0815
InTime = Apostrophe & Left(Target.Value, 2) & ":" & Right(Target.Value, 2) &
Apostrophe
Else: InTime = Apostrophe & "0" & Left(Target.Value, 1) & ":" &
Right(Target.Value, 2) & Apostrophe
End If
ActiveCell.Value = InTime
ActiveCell.NumberFormat = "hh:mm A"
End Sub


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
Lee Hunter
 
Posts: n/a
Default Input mask for active cell

Dave,

How absolutely simple and elegant! Thanks for the lesson. Much appreciated

Lee

"Dave Peterson" wrote:

First, you can't make up your own events. But you can tie into the
worksheet_Change event.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count 1 Then Exit Sub
If Intersect(Target, Me.Range("C:C")) Is Nothing Then Exit Sub

On Error GoTo errHandler:

If IsNumeric(Target.Value) = False Then Exit Sub
If Int(Target.Value) < Target.Value Then Exit Sub
If Target.Value < 0 Then Exit Sub

If Target.Value < 9999 Then
Application.EnableEvents = False
Target.Value = Format(Target.Value, "00:00")
Target.NumberFormat = "hh:mm AM/PM"
End If

errHandler:
Application.EnableEvents = True

End Sub

Notice the application.enableevents stuff. That's what stops the change to the
worksheet from calling the worksheet_change event.

You may want to take a look at how Chip Pearson approached the subject:
http://cpearson.com/excel/DateTimeEntry.htm






Lee Hunter wrote:

I am attempting to set up an input mask for the active cell such that the
time can be entered as 3 or 4 digits without the full colon.(815 for 08:15
AM)

In the sample code below, I never get to the numberformat property and the
code sems to be recursively called from itself. I've tried both the
Calculate and the Change events. What's my best solution?
Thanks!
Lee

Private Sub Worksheet_Some_event(ByVal Target As Range
Dim InTime As String
Dim Apostrophe As String
Apostrophe = ""
If Len(Target) = 4 Then ' Allow 815 instead of 0815
InTime = Apostrophe & Left(Target.Value, 2) & ":" & Right(Target.Value, 2) &
Apostrophe
Else: InTime = Apostrophe & "0" & Left(Target.Value, 1) & ":" &
Right(Target.Value, 2) & Apostrophe
End If
ActiveCell.Value = InTime
ActiveCell.NumberFormat = "hh:mm A"
End Sub


--

Dave Peterson

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
Help - Can't directly input Japanese characters into cell NKK Excel Discussion (Misc queries) 0 January 3rd 06 02:00 AM
Delete cell contents with input to adjacent cell Ashley Frank Excel Discussion (Misc queries) 1 October 5th 05 04:28 PM
How do I change the format of a cell based on what I input? Husker87 Excel Worksheet Functions 8 August 19th 05 10:45 PM
cell color index comparison MINAL ZUNKE New Users to Excel 1 June 30th 05 07:11 AM
what is the format for an input cell? Pumaman Excel Discussion (Misc queries) 2 February 8th 05 05:05 AM


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

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"