Using Split Function to add a hyphen to telephone number

met1713

New Member
Joined
May 27, 2015
Messages
10
I am trying to use a split function but it keeps saying subscript out of range. The input is 1234567 (any 7 digit number)
and the output needs to be 123-4567. so a user can enter their telephone number but it will add the hyphen for them. I can get it split when it has spaces but not without spaces. Need help
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I am trying to use a split function but it keeps saying subscript out of range. The input is 1234567 (any 7 digit number)
and the output needs to be 123-4567. so a user can enter their telephone number but it will add the hyphen for them. I can get it split when it has spaces but not without spaces. Need help

You wouldn't use the Split function for this, simply use the worksheet's REPLACE function instead...

Code:
Number = 1234567
HyphenNumber = WorksheetFunction.Replace(Number, Len(Number)-3, 0, "-")

The above assumes you phone number could possibly be in this form... (123)4567890 as well as 1234567, but if it will always be 7 digits long, then use this instead...

Code:
Number = 1234567
HyphenNumber = WorksheetFunction.Replace(Number, 4, 0, "-")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top