Pre-pend data to content of cells

ProvPC

Board Regular
Joined
Nov 2, 2014
Messages
62
Hi there!
I've a spreadsheet of 3500 rows that containing data that I'm preparing to import into tables in a database. Because the data was often inconsistently entered, there's a fair degree of manipulation to be done before it's ready to move to it's new home. So far (thanks to tygrrboi and Istvan Hirsch!) I've pretty much gotten the names separated and sorted out.

The next project is the phone numbers. I've manually taken care of nearly all the inconsistencies with formatting, but I'm left with one remaining issue, which sounds simple, but isn't working out the way I expected it to. I need to "pre-pend" a three-digit area code to the roughly 1500 numbers that don't already have them. In this case, it's presumed that all of the missing area codes are the same (208).

I thought I could do this with Find & Replace using wildcards (something along the lines of "find ###-#### and replace with (208) ###-####), but it doesn't seem to support wild cards. I could try to create a column of "208" and maybe use the "&" function to add the original number? But there's probably an easier way.

Any help would be appreciated!

Hm. Is "pre-pend" even a word?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,069
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Can you give us some examples of where the ###-#### string appears in your data entries?
 
Upvote 0

ProvPC

Board Regular
Joined
Nov 2, 2014
Messages
62
Can you give us some examples of where the ###-#### string appears in your data entries?

Happily!
Here is an idea of what I'm working with. The data in the phone column was entered in a variety of ways, with many combinations of country codes, area codes, spaces, dashes, parentheses, alphabetic notations - or lack of any of the above. I've pretty much sorted out the variances, so what's left are in the two formats shown in the example: (111) 111-2222 or just 111-2222.

What I'm hoping to find is a simple way to "pre-pend" the area code (which will always be 208 for those that are missing) to the phone numbers that don't already have an area code listed.
 
Upvote 0

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,069
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Happily!
Here is an idea of what I'm working with. The data in the phone column was entered in a variety of ways, with many combinations of country codes, area codes, spaces, dashes, parentheses, alphabetic notations - or lack of any of the above. I've pretty much sorted out the variances, so what's left are in the two formats shown in the example: (111) 111-2222 or just 111-2222.

What I'm hoping to find is a simple way to "pre-pend" the area code (which will always be 208 for those that are missing) to the phone numbers that don't already have an area code listed.
Will there ever be more than one "-" (as in ###-####) in a cell?
 
Upvote 0

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,069
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Will there ever be more than one "-" (as in ###-####) in a cell?
Have to go offline now so I'll assume there's only one hyphen in a cell, the one that separates ### from ####.

First, with your mouse, select all the cells that may need to be modified. Then run the macro below.
Code:
Sub PrefixAreaCode()
'Select the cells you want to apply this to, then run this macro
Dim c As Range
Const Pre As String = "(208)"
Application.ScreenUpdating = False
For Each c In Selection
       If c.Value Like "* ###-#### *" Or c.Value Like "###-#### *" Or c.Value _
           Like "* ###-####" Or c.Value Like "###-####" Then
                     c.Value = Replace(c.Value, Mid(c.Value, InStr(c.Value, "-") - 3, _
                         8), Pre & Mid(c.Value, InStr(c.Value, "-") - 3, 8))
       End If
Next c
Application.ScreenUpdating = False
End Sub
 
Upvote 0

ProvPC

Board Regular
Joined
Nov 2, 2014
Messages
62
Have to go offline now so I'll assume there's only one hyphen in a cell, the one that separates ### from ####.

First, with your mouse, select all the cells that may need to be modified. Then run the macro below.
Code:
Sub PrefixAreaCode()
'Select the cells you want to apply this to, then run this macro
Dim c As Range
Const Pre As String = "(208)"
Application.ScreenUpdating = False
For Each c In Selection
       If c.Value Like "* ###-#### *" Or c.Value Like "###-#### *" Or c.Value _
           Like "* ###-####" Or c.Value Like "###-####" Then
                     c.Value = Replace(c.Value, Mid(c.Value, InStr(c.Value, "-") - 3, _
                         8), Pre & Mid(c.Value, InStr(c.Value, "-") - 3, 8))
       End If
Next c
Application.ScreenUpdating = False
End Sub

I'm sorry for the delay! The holidays kept me away from the project. I'll give this a try - thanks for your help!
 
Upvote 0

ProvPC

Board Regular
Joined
Nov 2, 2014
Messages
62
Have to go offline now so I'll assume there's only one hyphen in a cell, the one that separates ### from ####.

First, with your mouse, select all the cells that may need to be modified. Then run the macro below.
Code:
Sub PrefixAreaCode()
'Select the cells you want to apply this to, then run this macro
Dim c As Range
Const Pre As String = "(208)"
Application.ScreenUpdating = False
For Each c In Selection
       If c.Value Like "* ###-#### *" Or c.Value Like "###-#### *" Or c.Value _
           Like "* ###-####" Or c.Value Like "###-####" Then
                     c.Value = Replace(c.Value, Mid(c.Value, InStr(c.Value, "-") - 3, _
                         8), Pre & Mid(c.Value, InStr(c.Value, "-") - 3, 8))
       End If
Next c
Application.ScreenUpdating = False
End Sub

I'm sorry to be a pest. I'm most likely doing something incorrectly, but when I select the cells that need updating and run the macro, nothing appears to happen. There are no errors of any kind, but the prefix is not added to the cell contents, Do you have any idea what I could be doing wrong?
 
Upvote 0

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,150
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I'm sorry to be a pest. I'm most likely doing something incorrectly, but when I select the cells that need updating and run the macro, nothing appears to happen. There are no errors of any kind, but the prefix is not added to the cell contents, Do you have any idea what I could be doing wrong?
I cannot help you with JoeMo's code, but I have an alternate macro for you to try. Note that you do not have to select any cells as it will process all the cells in Column E automatically.
Code:
[table="width: 500"]
[tr]
	[td]Sub PrefixAreaCode208()
  Dim Addr As String
  Addr = "E2:E" & Cells(Rows.Count, "E").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF(LEFT(@)=""("",@,""(208) ""&@)", "@", Addr))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

ProvPC

Board Regular
Joined
Nov 2, 2014
Messages
62
I cannot help you with JoeMo's code, but I have an alternate macro for you to try. Note that you do not have to select any cells as it will process all the cells in Column E automatically.
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub PrefixAreaCode208()
  Dim Addr As String
  Addr = "E2:E" & Cells(Rows.Count, "E").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF(LEFT(@)=""("",@,""(208) ""&@)", "@", Addr))
End Sub
[/TD]
[/TR]
</tbody>[/TABLE]

Hi there! Thank you for your response!
This works very well indeed. I appreciate your help!
 
Upvote 0

Forum statistics

Threads
1,191,517
Messages
5,987,057
Members
440,074
Latest member
Emmanuelian

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
Top