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


The code below is designed to change a number input, such as 1425 to 14:25.
Is there anything I can do prevent the code from running if the number is
already properly inputted?


Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim e

ActiveSheet.Unprotect

On Error GoTo ErrorHandler

If Not Application.Intersect(Target, Range("E3:E8000")) Is Nothing Then

If ActiveCell < "" Then GoTo ErrorHandler

e = Left(Format(Target.Value, "0000"), 4)
Application.EnableEvents = False
Target.Formula = Left(e, 2) & ":" & Right(e, 2)

End If


Application.EnableEvents = True

If ActiveCell = ":" Then GoTo ClearCell

Exit Sub

ClearCell:

Selection.ClearContents

Exit Sub

ErrorHandler:


Exit Sub:

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default If statement question

Would it help to use the InStr function? This is what I usually do if
I understand your question correctly. You could use something like . .
..

Dim lkFor as Integer
set nextCell = myCell.Offset(1,0)
lkFor = InStr(myCell, ":")
If lkFor 0 then
' now you know the colon character exists and handle accordingly
set myCell = nextCell
End If

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,726
Default If statement question

Test it for numeric

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim e

Application.EnableEvents = False
Sh.Unprotect
On Error GoTo ErrorHandler

If Not Application.Intersect(Target, Range("E3:E8000")) Is Nothing Then

If Not IsNumeric(Target.Value) Then GoTo ErrorHandler

e = Left(Format(Target.Value, "0000"), 4)
Target.Formula = Left(e, 2) & ":" & Right(e, 2)

ElseIf Target.Value = ":" Then GoTo ClearCell

Exit Sub

ClearCell:
Target.ClearContents
Exit Sub

ErrorHandler:

Exit Sub:
Application.EnableEvents = True

End Sub

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)


"Patrick Simonds" wrote in message
...

The code below is designed to change a number input, such as 1425 to
14:25. Is there anything I can do prevent the code from running if the
number is already properly inputted?


Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As
Range)
Dim e

ActiveSheet.Unprotect

On Error GoTo ErrorHandler

If Not Application.Intersect(Target, Range("E3:E8000")) Is Nothing Then

If ActiveCell < "" Then GoTo ErrorHandler

e = Left(Format(Target.Value, "0000"), 4)
Application.EnableEvents = False
Target.Formula = Left(e, 2) & ":" & Right(e, 2)

End If


Application.EnableEvents = True

If ActiveCell = ":" Then GoTo ClearCell

Exit Sub

ClearCell:

Selection.ClearContents

Exit Sub

ErrorHandler:


Exit Sub:

End Sub



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
IF - And - Then Statement question MayJuneJuly Excel Worksheet Functions 4 September 19th 08 04:00 PM
IF Statement question peg84 Excel Discussion (Misc queries) 3 February 4th 08 10:19 PM
IF statement question [email protected] Excel Worksheet Functions 2 July 24th 06 04:20 PM
IF Statement Question Scott Excel Worksheet Functions 15 June 14th 05 07:56 PM
IF Statement question gryfon Excel Worksheet Functions 6 May 4th 05 09:21 AM


All times are GMT +1. The time now is 11:49 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"