ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   What's wrong with my code? Thanks (https://www.excelbanter.com/excel-programming/388074-whats-wrong-my-code-thanks.html)

Exceller

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


Bob Phillips

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




Exceller

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





Norman Jones

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



Exceller

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





All times are GMT +1. The time now is 10:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com