Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default help with code!

I cannot get my code to work! i am trying to just check
that when the user enters a value in cell Y13 that its a 9
digit numeric value

but every time i enter a value. it always goes to the last
condition "Must be 9 digits" even when i enter in a
correct value

what's wrong here?! help! :)

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("Y13")) Is Nothing
Then
With Target
'do something
If Len(Y13) = 9 Then
If IsNumeric(Y13) Then
Y13.NumberFormat = "###-###-###"
Else
MsgBox "Must be a number"
End If
Else
MsgBox "Must be 9 digits"
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 134
Default help with code!


Instead of nesting your if statements, try do it
sequentially. Example:

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("Y13")) Is Nothing
Then
With Target
'do something
If Len(Y13) < 9 Then
MsgBox "Must be 9 digits"
Exit Sub
End If

If IsNumeric(Y13) Then
Y13.NumberFormat = "###-###-###"
Else
MsgBox "Must be a number"
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

Not sure if that will do it, but logically it will still
flow through the proper validation steps.
-----Original Message-----
I cannot get my code to work! i am trying to just check
that when the user enters a value in cell Y13 that its a

9
digit numeric value

but every time i enter a value. it always goes to the

last
condition "Must be 9 digits" even when i enter in a
correct value

what's wrong here?! help! :)

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("Y13")) Is Nothing
Then
With Target
'do something
If Len(Y13) = 9 Then
If IsNumeric(Y13) Then
Y13.NumberFormat = "###-###-###"
Else
MsgBox "Must be a number"
End If
Else
MsgBox "Must be 9 digits"
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub


.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default help with code!

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Y13 as Range
On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("Y13")) Is Nothing Then
set Y13 = Target
'do something
If Len(Y13) = 9 Then
If IsNumeric(Y13) Then
Y13.NumberFormat = "###-###-###"
Else
MsgBox "Must be a number"
End If
Else
MsgBox "Must be 9 digits"
End If
End If

ws_exit:
Application.EnableEvents = True
End Sub

--
Regards,
Tom Ogilvy


wrote in message
...
I cannot get my code to work! i am trying to just check
that when the user enters a value in cell Y13 that its a 9
digit numeric value

but every time i enter a value. it always goes to the last
condition "Must be 9 digits" even when i enter in a
correct value

what's wrong here?! help! :)

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("Y13")) Is Nothing
Then
With Target
'do something
If Len(Y13) = 9 Then
If IsNumeric(Y13) Then
Y13.NumberFormat = "###-###-###"
Else
MsgBox "Must be a number"
End If
Else
MsgBox "Must be 9 digits"
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default help with code!

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Y13 as Range
On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("Y13")) Is Nothing Then
Set Y13 = Target
'do something
If Len(Y13) < 9 Then
MsgBox "Must be 9 digits"
Exit Sub
End If

If IsNumeric(Y13) Then
Y13.NumberFormat = "###-###-###"
Else
MsgBox "Must be a number"
End If
End If

ws_exit:
Application.EnableEvents = True
End Sub

--
Regards,
Tom Ogilvy


"Kevin" wrote in message
...

Instead of nesting your if statements, try do it
sequentially. Example:

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("Y13")) Is Nothing
Then
With Target
'do something
If Len(Y13) < 9 Then
MsgBox "Must be 9 digits"
Exit Sub
End If

If IsNumeric(Y13) Then
Y13.NumberFormat = "###-###-###"
Else
MsgBox "Must be a number"
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

Not sure if that will do it, but logically it will still
flow through the proper validation steps.
-----Original Message-----
I cannot get my code to work! i am trying to just check
that when the user enters a value in cell Y13 that its a

9
digit numeric value

but every time i enter a value. it always goes to the

last
condition "Must be 9 digits" even when i enter in a
correct value

what's wrong here?! help! :)

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("Y13")) Is Nothing
Then
With Target
'do something
If Len(Y13) = 9 Then
If IsNumeric(Y13) Then
Y13.NumberFormat = "###-###-###"
Else
MsgBox "Must be a number"
End If
Else
MsgBox "Must be 9 digits"
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub


.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default help with code!

"Error" isn't very informative. If that is sufficient, then good
suggestion.

--
Regards,
Tom Ogilvy

"Don Guillett" wrote in message
...
Your first if is doing just that. Why not simplify it with something like.


if target.address< "$Y$13" then exit sub

if Len(target) = 9 and IsNumeric(target) Then
target.NumberFormat = "###-###-###"
Else
MsgBox "Error"
End If



--
Don Guillett
SalesAid Software

wrote in message
...
I cannot get my code to work! i am trying to just check
that when the user enters a value in cell Y13 that its a 9
digit numeric value

but every time i enter a value. it always goes to the last
condition "Must be 9 digits" even when i enter in a
correct value

what's wrong here?! help! :)

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("Y13")) Is Nothing
Then
With Target
'do something
If Len(Y13) = 9 Then
If IsNumeric(Y13) Then
Y13.NumberFormat = "###-###-###"
Else
MsgBox "Must be a number"
End If
Else
MsgBox "Must be 9 digits"
End If
End With
End If

ws_exit:
Application.EnableEvents = True
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
split post code (zip code) out of cell that includes full address Concord Excel Discussion (Misc queries) 4 October 15th 09 06:59 PM
Code to conditional format all black after date specified in code? wx4usa Excel Discussion (Misc queries) 3 December 26th 08 07:06 PM
Drop Down/List w/Code and Definition, only code entered when selec Spiritdancer Excel Worksheet Functions 2 November 2nd 07 03:57 AM
option buttons run Click code when value is changed via VBA code neonangel Excel Programming 5 July 27th 04 08:32 AM
VBA code delete code but ask for password and unlock VBA protection WashoeJeff Excel Programming 0 January 27th 04 07:07 AM


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