Mister H said:
Hi:
I am trying to work with this code. I have a couple of questions. How do I add a second Range to this code? I have 2 Ranges to use this coding on.
Also, when I use this code it seems to work however, when I delete the number out of the cell it puts in
000-00000-0000-000000-000000-0000-0000 (it will not allow me to remove the 0's which poses a problem when I use my Delete Rows Macro).
Can this code be altered to leave the cell blank when it is:
000-00000-0000-000000-000000-0000-0000
THANKS for your assistance and time,
Mark
You *really* need to stay on a single post per topic.
<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
<SPAN style="color:#00007F">Dim</SPAN> Rng <SPAN style="color:#00007F">As</SPAN> Range, TempStr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, c <SPAN style="color:#00007F">As</SPAN> Range
<SPAN style="color:#00007F">Set</SPAN> Rng = Union([A1:D10], [X1:Z10]) <SPAN style="color:#007F00">' Change A1:D10 to the range you want to monitor</SPAN>
<SPAN style="color:#00007F">If</SPAN> Intersect(Target, Rng) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
<SPAN style="color:#00007F">Const</SPAN> NUM_LEN = 33 <SPAN style="color:#007F00">' this is the length of your value, less the dashes</SPAN>
Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">Set</SPAN> Rng = Intersect(Target, Rng)
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> Rng
<SPAN style="color:#00007F">If</SPAN> Len(c.Value) > 0 <SPAN style="color:#00007F">Then</SPAN>
<SPAN style="color:#00007F">With</SPAN> Application.WorksheetFunction
TempStr = c.Value & .Rept("0", .Max(NUM_LEN - Len(c.Value), 0))
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
c.Value = Left$(TempStr, 3) & "-" & Mid$(TempStr, 4, 5) & "-" & Mid$(TempStr, 10, 4) _
& "-" & Mid$(TempStr, 14, 6) & "-" & Mid$(TempStr, 20, 6) & "-" & Mid$(TempStr, 26, 4) _
& "-" & Right$(TempStr, 4)
<SPAN style="color:#00007F">Else</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">Next</SPAN> c
Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">Set</SPAN> Rng = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>