View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Insert "/" into cells that contain data, using a macro

On Wed, 13 Feb 2008 11:11:01 -0800, garfieldmark00
wrote:

That's exactly what I needed. Many thanks Ron


You're welcome. Glad to help. Thanks for the feedback.

Note that the macro does not check that you have a valid entry. It will work
on any string where 5 digits are preceded by two or more letters or digits (or
underscores).

It would be trivial to change the code to ensure it works only on valid
entries, but more information on what constitutes a valid entry would be
required to do that.

For example, if the ONLY characters in the cell were two initial letters,
without regard to case, followed by 10 digits, then:

=========================
Sub InsertSlash()
Dim c As Range
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = "(^[A-Za-z]{2})(\d{5})(\d{5})$"
For Each c In Selection
c.Value = re.Replace(c.Value, "$1/$2/$3")
Next c
End Sub
=========================

would process valid entries, and leave invalid entries unchanged.

You could also set conditions such as the relevant string being case sensitive;
being found anywhere within the cell; and you could set results to be just the
relevant processed string; an unprocessed string; an error message if the
pattern is not found; etc.

With all these permutations, I kept it pretty simple for an initial trial.
--ron