Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 123
Default Conversion of 10 digit number into xx-xxx-xx-xxx

Dear Experts:

I got a column of 10-digit numbers. They need to be converted like
this .....

Befo xxxxxxxxxx (10 digits)
After: xx-xxx-xx-xxx (10 digits separated by hyphens)

I would like to run a macro that does this conversion on selected
cells.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 220
Default Conversion of 10 digit number into xx-xxx-xx-xxx

Try this. It will allow for multiple selections anywhere on the sheet. It
only addresses the first column of each selection, though. Enter the macro
in a general code module in the Visual Basic Editor.

Option Explicit

Sub Convert_Phone_Numbers()
Dim nAreas As Long, nRows As Long, i As Long, j As Long
Dim oldText As String, newText As String
'
nAreas = Selection.Areas.Count
'
For i = 1 To nAreas
nRows = Selection.Areas(i).Rows.Count
For j = 1 To nRows
oldText = Selection.Areas(i).Cells(j, 1).Text
newText = Left(oldText, 3) & "-" & Mid(oldText, 4, 3) & "-" &
Right(oldText, 4)
Selection.Areas(i).Cells(j, 1) = newText
Next j
Next i
End Sub

HTH,

Eric


"andreashermle" wrote:

Dear Experts:

I got a column of 10-digit numbers. They need to be converted like
this .....

Befo xxxxxxxxxx (10 digits)
After: xx-xxx-xx-xxx (10 digits separated by hyphens)

I would like to run a macro that does this conversion on selected
cells.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 905
Default Conversion of 10 digit number into xx-xxx-xx-xxx

"andreashermle" wrote:
I got a column of 10-digit numbers. They need
to be converted like this .....
Befo xxxxxxxxxx (10 digits)
After: xx-xxx-xx-xxx


Are the original contents ("before") bona fide numbers (i.e. TYPE(A1)=1), or
they strings (text) of digits (i.e. TYPE(A1)=2)?

If they are bona fide numbers, and if it is sufficient for them to simply
appear as you wish, you might try using the Custom format 00-000-00-000 .

If they are text, it might be sufficient for you to put the following
formula into a parallel column: =TEXT(A1,"00-000-00-00") . If you wish,
you can replace the original column with the results of the new column by
copying the new column, then using paste-special-value to overwrite the
original column. Then you can delete the new column.

Finally, if you truly wish to use a macro, perhaps the following will do the
trick. Select all of the cells with 10-digit numbers. Then execute the
following macro:

Option Explicit
Sub doit()
Dim c As Range
For Each c In Selection
c = Format(c, "00-000-00-000")
Next c
Selection.Columns.AutoFit
End Sub


----- original message -----

"andreashermle" wrote in message
...
Dear Experts:

I got a column of 10-digit numbers. They need to be converted like
this .....

Befo xxxxxxxxxx (10 digits)
After: xx-xxx-xx-xxx (10 digits separated by hyphens)

I would like to run a macro that does this conversion on selected
cells.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 123
Default Conversion of 10 digit number into xx-xxx-xx-xxx

On Jan 18, 11:48*pm, EricG wrote:
Try this. *It will allow for multiple selections anywhere on the sheet. *It
only addresses the first column of each selection, though. *Enter the macro
in a general code module in the Visual Basic Editor.

Option Explicit

Sub Convert_Phone_Numbers()
* * Dim nAreas As Long, nRows As Long, i As Long, j As Long
* * Dim oldText As String, newText As String
'
* * nAreas = Selection.Areas.Count
'
* * For i = 1 To nAreas
* * * * nRows = Selection.Areas(i).Rows.Count
* * * * For j = 1 To nRows
* * * * * * oldText = Selection.Areas(i).Cells(j, 1).Text
* * * * * * newText = Left(oldText, 3) & "-" & Mid(oldText, 4, 3) & "-" &
Right(oldText, 4)
* * * * * * Selection.Areas(i).Cells(j, 1) = newText
* * * * Next j
* * Next i
End Sub

HTH,

Eric



"andreashermle" wrote:
Dear Experts:


I got a column of 10-digit numbers. They need to be converted like
this .....


Befo xxxxxxxxxx (10 digits)
After: xx-xxx-xx-xxx (10 digits separated by hyphens)


I would like to run a macro that does this conversion on selected
cells.


Help is much appreciated. Thank you very much in advance.


Regards, Andreas
.- Hide quoted text -


- Show quoted text -


Hi Eric,

great coding. Exactly what I wanted. Had to alter the code slightly to
suit my needs. Thank you very much for your professional help.
Regards, Andreas
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 123
Default Conversion of 10 digit number into xx-xxx-xx-xxx

On Jan 19, 12:20*am, "Joe User" <joeu2004 wrote:
"andreashermle" wrote:
I got a column of 10-digit numbers. They need
to be converted like this .....
Befo xxxxxxxxxx (10 digits)
After: xx-xxx-xx-xxx


Are the original contents ("before") bona fide numbers (i.e. TYPE(A1)=1), or
they strings (text) of digits (i.e. TYPE(A1)=2)?

If they are bona fide numbers, and if it is sufficient for them to simply
appear as you wish, you might try using the Custom format 00-000-00-000 .

If they are text, it might be sufficient for you to put the following
formula into a parallel column: *=TEXT(A1,"00-000-00-00") . *If you wish,
you can replace the original column with the results of the new column by
copying the new column, then using paste-special-value to overwrite the
original column. *Then you can delete the new column.

Finally, if you truly wish to use a macro, perhaps the following will do the
trick. *Select all of the cells with 10-digit numbers. *Then execute the
following macro:

Option Explicit
Sub doit()
Dim c As Range
For Each c In Selection
c = Format(c, "00-000-00-000")
Next c
Selection.Columns.AutoFit
End Sub

----- original message -----

"andreashermle" wrote in message

...



Dear Experts:


I got a column of 10-digit numbers. They need to be converted like
this .....


Befo xxxxxxxxxx (10 digits)
After: xx-xxx-xx-xxx (10 digits separated by hyphens)


I would like to run a macro that does this conversion on selected
cells.


Help is much appreciated. Thank you very much in advance.


Regards, Andreas- Hide quoted text -


- Show quoted text -


Hi Joe,

thank you very much for the macro and non-macro solution. Both work
just fine. Thank you very much for your professional help. Regards,
Andreas
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 to validate a cell to insert from 9 digit number to 13 digit number. ramulu via OfficeKB.com Excel Worksheet Functions 1 February 21st 07 02:32 PM
How to validate a cell to insert from 9 digit number to 13 digit number. ramulu Excel Worksheet Functions 1 February 21st 07 10:00 AM
Sorting a six digit number by terminal digit Brian Excel Worksheet Functions 10 August 11th 06 06:50 PM
Color a single digit in a mult-digit number cell Phyllis Excel Discussion (Misc queries) 6 November 17th 05 12:46 AM
When we enter a 16 digit number (credit card) the last digit chan. ceking Excel Discussion (Misc queries) 5 December 8th 04 11:45 PM


All times are GMT +1. The time now is 07:09 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"