R
Rick Rothstein \(MVP - VB\)
That Range("X") was supposed to have been Range("X:X"). Try changing the
line to this and see if it works...
If Target.Count > 1 Or Target.Row = 1 Or Len(S) = 0 Or _
Intersect(Target, Union(Range("S:V"), Range("X:X"), Range("AK:BD"))) _
Is Nothing Then Exit Sub
Rick
line to this and see if it works...
If Target.Count > 1 Or Target.Row = 1 Or Len(S) = 0 Or _
Intersect(Target, Union(Range("S:V"), Range("X:X"), Range("AK:BD"))) _
Is Nothing Then Exit Sub
Rick
Minitman said:Good morning Rick,
Good to hear from you again.
There seems to be a problem with this code. It hangs up on the on the
if statement at this place:
With the error message:
Run-time error '1004':
Method 'Range" of object '_Worksheet' failed
Debug highlighted this line:
If Target.Count > 1 Or Target.Row = 1 Or Len(S) = 0 Or _
Intersect(Target, Union(Range("S:V"), Range("X"),
Range("AK:BD"))) _
Is Nothing Then Exit Sub
Breaking up this line at the 'Or's, I was able to eliminate all but
this code snippet:
...Union(Range("S:V"), Range("X"), Range("AK:BD"))...
Which looks good to me but not to debug.
I'm not sure if I did this elimination process right or not, but that
was all I could think of to try.
Other then that, I got no further.
Is there an easy fix?
Please let me know.
Thanks.
-Minitman
Going back to my construction... does this do what you want?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim X As Long
Dim S As String
Dim bFlag As Boolean
S = Target.Value
If Target.Count > 1 Or Target.Row = 1 Or Len(S) = 0 Or _
Intersect(Target, Union(Range("S:V"), Range("X"), Range("AK:BD"))) _
Is Nothing Then Exit Sub
For X = 1 To Len(S)
If Target.Column = 24 And Mid(S, X, 1) Like "[!0-9a-zA-Z]" Then
Mid(S, X, 1) = " "
ElseIf Mid(S, X, 1) Like "[!0-9]" Then
Mid(S, X, 1) = " "
End If
Next
S = Replace(S, " ", "")
Select Case Target.Column
Case 24 'MapsCo Formatting
S = Format(S, ">!Map @@@@ \<@@-@@\>")
Case 19, 21, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55 'Telephone format
If Len(S) = 7 Then
S = Format(S, "000-0000")
ElseIf Len(S) = 10 Then
S = Format(S, "(000) 000-0000")
Else
Exit Sub
End If
Case 20, 22, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56 'Extension format
S = Format(S, "Ext " & String(Len(S), "0"))
End Select
On Error GoTo EndIt
Application.EnableEvents = False
Target.Value = S
EndIt:
Application.EnableEvents = True
End Sub
Rick
Minitman said:Hey Ron,
Two out of three really work well, Thank you.
The MapsCo (column 24) lost all of it alpha characters.
In response to your question, the phone numbers are treated as text.
As is the MapsCo string.
The MapsCo data consist of 3 digits with three letters followed by two
digits for the eight base characters. After formatting it appears as
Map 000@ <@@-00>
Example:
Data: 426rmk24
Formatted: Map 426R <MK-24>
The code removed all of the alpha characters along with all non number
characters. I need those alpha characters.
I can't seem to figure out where to put the MapsCo formatting code
without stripping out the alpha characters. The re.Pattern = "\D+"
seems to be the problem. How do I strip everything but the
alpha-numeric characters and change all alpha characters to lower
case? And then apply the formatting.
Any ideas:
-Minitman
On Fri, 13 Jun 2008 22:18:34 -0400, Ron Rosenfeld
On Fri, 13 Jun 2008 19:14:02 -0500, Minitman
<[email protected]>
wrote:
I had left out the Case 24 (the formatting that Peter T came up with
using Rick's "voodoo" formatting trick back in Jul 10, 2007) since I
thought it would be a less cluttered post and that it should be a
simple matter to reintegrate it into the final code, silly me.
But I don't understand vbscript or what is actually happening! I'm a
little afraid to start modifying code I don't understand!
Are there any special tricks that I should be aware of when attempting
to utilize and or modify your suggestions?
1. Rick's routine returns your result as a text string. Mine and Doug's
return
a number formatted as a telephone number or extension. They would both
appear
the same in the cell -- but Text and Numbers will behave differently in
formulas.
2. You would have to add the Column 24 to my list of both an acceptable
Target
and also for a different format. Could you give an example of what it
would
look like? And does the data in Column 24 also require removal of all
non-digits?
If so, mine is easily modified to something like:
================================
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rTel As Range, rExt As Range, c As Range
Dim col As Object
Dim rMapsCo As Range
Dim re As Object
Set rTel = Union(Columns(19), Columns(21), Columns(37), Columns(39), _
Columns(41), Columns(43), Columns(45), Columns(47), _
Columns(49), Columns(51), Columns(53), Columns(55))
Set rExt = Union(Columns(20), Columns(22), Columns(38), Columns(42), _
Columns(44), Columns(46), Columns(48), Columns(50), _
Columns(52), Columns(54), Columns(56))
Set rMapsCo = Columns(24)
If Not Intersect(Target, Union(rTel, rExt, rMapsCo)) Is Nothing Then
Application.EnableEvents = False
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "\D+"
For Each c In Target
If Not Intersect(c, rTel) Is Nothing _
And c.Row <> 1 Then
c.Value = re.Replace(c.Value, "")
c.NumberFormat = "[<=9999999]###-####;(###) ###-####"
End If
If Not Intersect(c, rExt) Is Nothing _
And c.Row <> 1 Then
c.Value = re.Replace(c.Value, "")
c.NumberFormat = """Ext. ""0"
End If
If Not Intersect(c, rMapsCo) Is Nothing _
And c.Row <> 1 Then
c.Value = re.Replace(c.Value, "")
c.NumberFormat = """>!Map ""0000 ""\<""00-00""\>"""
End If
Next c
Application.EnableEvents = True
End If
End Sub
=================================
IF you prefer a text string output, then you can change the lines that
output
the values, as in below:
============================
...
For Each c In Target
If Not Intersect(c, rTel) Is Nothing _
And c.Row <> 1 Then
c.Value = Application.WorksheetFunction.Text _
(re.Replace(c.Value, ""), "[<=9999999]###-####;(###)
###-####")
End If
If Not Intersect(c, rExt) Is Nothing _
And c.Row <> 1 Then
c.Value = Application.WorksheetFunction.Text _
(re.Replace(c.Value, ""), """Ext. ""0")
End If
If Not Intersect(c, rMapsCo) Is Nothing _
And c.Row <> 1 Then
c.Value = Application.WorksheetFunction.Text _
(re.Replace(c.Value, ""), """>!Map ""0000
""\<""00-00""\>""")
End If
Next c
...
==========================================
Also, for each segment (telephone, extension, MapsCo) you could test
each
result for proper data, depending on the requirements, as I mentioned
before.
If you have questions about the various code segments, feel free to ask.
In particular the Regular Expression pattern "\D+" refers to any
characters in
the string that are not digits (i.e. not in the set [0-9]). the Replace
methods replaces all matches (all non-digits) with a null string.
The rest is pretty straightforward.
--ron