Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 55
Default What's wrong with my code? Thanks

I'm using the following macro to format phone numbers from "1234567890" to
"123-456-7890". While this works fine on single cells when I try it on a list
it deletes the numbers and inserts the dashes "--".
What is wrong with this? Thanks.

Sub InsertDashes()
Set r = Selection
v = r.Text
v = Left(v, 3) & "-" & Mid(v, 4, 3) & "-" & Right(v, 4)
r.Value = v
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default What's wrong with my code? Thanks

Keeping your style of code

Sub InsertDashes()
Dim cell as Range
Set r = Selection
For Each cell In r
v = cell.Text
v = Left(v, 3) & "-" & Mid(v, 4, 3) & "-" & Right(v, 4)
cell.Value = v
Next cell
End Sub


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Exceller" wrote in message
...
I'm using the following macro to format phone numbers from "1234567890" to
"123-456-7890". While this works fine on single cells when I try it on a
list
it deletes the numbers and inserts the dashes "--".
What is wrong with this? Thanks.

Sub InsertDashes()
Set r = Selection
v = r.Text
v = Left(v, 3) & "-" & Mid(v, 4, 3) & "-" & Right(v, 4)
r.Value = v
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 55
Default What's wrong with my code? Thanks

Ahhhh--that darn "Dim cell as Range" trick. That gets me every time.
Works like a charm. Thanks, Bob!
Regards,
Exceller

"Bob Phillips" wrote:

Keeping your style of code

Sub InsertDashes()
Dim cell as Range
Set r = Selection
For Each cell In r
v = cell.Text
v = Left(v, 3) & "-" & Mid(v, 4, 3) & "-" & Right(v, 4)
cell.Value = v
Next cell
End Sub


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Exceller" wrote in message
...
I'm using the following macro to format phone numbers from "1234567890" to
"123-456-7890". While this works fine on single cells when I try it on a
list
it deletes the numbers and inserts the dashes "--".
What is wrong with this? Thanks.

Sub InsertDashes()
Set r = Selection
v = r.Text
v = Left(v, 3) & "-" & Mid(v, 4, 3) & "-" & Right(v, 4)
r.Value = v
End Sub




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default What's wrong with my code? Thanks

Hi Exceller,

Ahhhh--that darn "Dim cell as Range" trick. That gets me every time.
Works like a charm. Thanks, Bob!


Also note Bob's For Each .. Next loop.


---
Regards,
Norman


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 55
Default What's wrong with my code? Thanks

Good point. Thanks, Norman.
Exceller


"Norman Jones" wrote:

Hi Exceller,

Ahhhh--that darn "Dim cell as Range" trick. That gets me every time.
Works like a charm. Thanks, Bob!


Also note Bob's For Each .. Next loop.


---
Regards,
Norman



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
Is there something wrong with my code? Corey Excel Programming 5 February 19th 07 02:29 PM
What is wrong with this code? sungen99[_32_] Excel Programming 3 October 12th 05 08:12 PM
What is wrong with this code? Oreg[_39_] Excel Programming 0 October 12th 05 07:00 PM
What's wrong with this code, please? Jim Berglund Excel Programming 1 August 3rd 04 09:41 PM
What's wrong w/my code? nrage21[_54_] Excel Programming 5 July 28th 04 03:50 PM


All times are GMT +1. The time now is 06:51 AM.

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"