Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Insert "/" into cells that contain data, using a macro

I need to insert a "/" symbol into multiple cells which already contain data.
For example: the cell already contains 2 letters and 10 digits and I need to
insert the / symbol after the 2 letters and then again after the next 5
digits, so the data changes from aa########## to aa/#####/#####. All the
cells are in one column so it's simple to get a macro to select the column,
but I don't know how to insert characters. Any ideas?
I'm using Excel 2007.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Insert "/" into cells that contain data, using a macro

Sub fixum()
s = "/"
For Each r In Selection
v = r.Value
r.Value = Left(v, 2) & s & Mid(v, 3, 5) & s & Right(v, 5)
Next
End Sub

--
Gary''s Student - gsnu200768


"garfieldmark00" wrote:

I need to insert a "/" symbol into multiple cells which already contain data.
For example: the cell already contains 2 letters and 10 digits and I need to
insert the / symbol after the 2 letters and then again after the next 5
digits, so the data changes from aa########## to aa/#####/#####. All the
cells are in one column so it's simple to get a macro to select the column,
but I don't know how to insert characters. Any ideas?
I'm using Excel 2007.

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

On Sun, 10 Feb 2008 09:48:01 -0800, garfieldmark00
wrote:

I need to insert a "/" symbol into multiple cells which already contain data.
For example: the cell already contains 2 letters and 10 digits and I need to
insert the / symbol after the 2 letters and then again after the next 5
digits, so the data changes from aa########## to aa/#####/#####. All the
cells are in one column so it's simple to get a macro to select the column,
but I don't know how to insert characters. Any ideas?
I'm using Excel 2007.


Using regular expressions, something like:

==========================
Option Explicit
Sub InsertSlash()
Dim c As Range
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = "(\w{2})(\d{5})"
For Each c In Selection
c.Value = re.Replace(c.Value, "$1/$2/")
Next c
End Sub
=================================


--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Insert "/" into cells that contain data, using a macro

You didn't say if the contents of every cell were of exactly the same shape
(2 letters followed by 10 digits) or not; but your use of the words "for
example" made me think not. So here is a general solution for any number of
leading letters and any number of trailing digits...

Sub InsertSlashes()
Dim C As Range
Dim X As Long
Dim LetterCount As Long
For Each C In Selection
For X = 1 To Len(C.Value)
If Mid$(C.Value, X, 1) Like "#" Then
LetterCount = X - 1
Exit For
End If
Next
If LetterCount 0 And C.Value Like Replace(String$(LetterCount, _
Chr$(0)), Chr$(0), "[a-zA-Z]") & _
String$(Len(C.Value) - LetterCount, "#") Then
C.Value = Left$(C.Value, LetterCount) & _
Format$(Mid$(C.Value, LetterCount + 1), "!" & _
Replace(String$(-((Len(C.Value) - LetterCount) _
Mod 5 < 0) + (Len(C.Value) - LetterCount) \ 5, _
Chr$(0)), Chr$(0), "\/&&&&&"))
End If
LetterCount = 0
Next
End Sub

Note: If there aren't any leading letters or any trailing digits or the
shape is anything but leading letters followed by trailing digits, then the
contents of the cell will remain unchanged.

To use this subroutine, select the cells you want to change (do not select
the whole column or you will be waiting quite a long time for the empty
cells to be processed), press Alt+F8 and execute the InsertSlashes macro

Rick


"garfieldmark00" wrote in message
...
I need to insert a "/" symbol into multiple cells which already contain
data.
For example: the cell already contains 2 letters and 10 digits and I need
to
insert the / symbol after the 2 letters and then again after the next 5
digits, so the data changes from aa########## to aa/#####/#####. All the
cells are in one column so it's simple to get a macro to select the
column,
but I don't know how to insert characters. Any ideas?
I'm using Excel 2007.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Insert "/" into cells that contain data, using a macro

That's exactly what I needed. Many thanks Ron

"Ron Rosenfeld" wrote:

On Sun, 10 Feb 2008 09:48:01 -0800, garfieldmark00
wrote:

I need to insert a "/" symbol into multiple cells which already contain data.
For example: the cell already contains 2 letters and 10 digits and I need to
insert the / symbol after the 2 letters and then again after the next 5
digits, so the data changes from aa########## to aa/#####/#####. All the
cells are in one column so it's simple to get a macro to select the column,
but I don't know how to insert characters. Any ideas?
I'm using Excel 2007.


Using regular expressions, something like:

==========================
Option Explicit
Sub InsertSlash()
Dim c As Range
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = "(\w{2})(\d{5})"
For Each c In Selection
c.Value = re.Replace(c.Value, "$1/$2/")
Next c
End Sub
=================================


--ron



  #6   Report Post  
Posted to microsoft.public.excel.programming
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
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
How do I "insert copied cells"? used to be "alt i e" still work Inserter Excel Worksheet Functions 1 February 14th 09 10:06 PM
Macro to concatenate into "B1" B2 thru B"x" based on new data in "Col A" Dennis Excel Discussion (Misc queries) 0 July 17th 06 02:38 PM
Insert "-" in text "1234567890" to have a output like this"123-456-7890" Alwyn Excel Discussion (Misc queries) 3 October 25th 05 11:36 PM
Insert 19 cells "Shift to the right" if cell contains "-" robertjtucker[_4_] Excel Programming 5 July 25th 05 04:20 PM
Adding "New" "Insert" "Delete" into a workbook to change from data 1 to data 2 etc Bob Reynolds[_2_] Excel Programming 0 March 4th 04 08:52 PM


All times are GMT +1. The time now is 09:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"