View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Gareth[_6_] Gareth[_6_] is offline
external usenet poster
 
Posts: 158
Default Excel treating text as function

Hi Bill,

You might want to swap those EnableEvents around: False first, then True
- and insert them inside the IF/End IF. (I'm sure you know that.)

You could instruct your users to insert a space before the dash - if you
think they'll find it easier. Then you could clean it up with something
like:

If Left$(Target.Value, 1) = " " Then
Application.EnableEvents = False
Target.Value = LTrim(Target.Value)
'optionally...
Target.NumberFormat = "@"
Application.EnableEvents = True
End If

Personally, I'd train them to use the apostrophe as Earl suggested if
you can though. It's quick and useful for them to know in general Excel use.

Q: Why do they need to enter dashes anyway? Is it just to indicate
they've left that cell blank of something? In which case you may be
better off having them insert a space and changing it through code using
something like:

If Target.Value = " " Or Target.Value = " " Then
Application.EnableEvents = False
Target.Value = "'-"
Application.EnableEvents = True
End If

cya,
G

Bill P wrote:
Thanks for the help. I've developed a workaround that supresses the
function error message, but does not produce the ideal behavior.

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = True

If Strings.Left(Target.Value, 1) = "-" Then
Target.Value = "-"
Exit Sub
End If

Application.EnableEvents = False

' Rest of validation...
End Sub

It sounds like it may not be possible to prevent Excel from treating
that as a formula. Oh well...

- Bill