ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If statement question (https://www.excelbanter.com/excel-programming/380418-if-statement-question.html)

Patrick Simonds

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



MrScience

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


Bob Phillips

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





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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com