Conditional replace part of a numeric or string value

Nite0wls

New Member
Joined
May 21, 2014
Messages
33
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Dear All,

I have a large sheet that does contain usage date of telephone numbers.

To uniform all numbers to an normalized format so I need to replace nrs starting with 56xx to nr 852392556xx
There are many more changes that I need to make so I use a macro for this that does all the conversions.

The problem that I come across is that when I do a replace of 56 to 852392556 all 56 entries form a match for this so

913560344 is transformed to 913852392556 0344
5693 is transformed to 85239255693
5656 is transformed to 852392556852392556
and so on.

Is there a way to use regex '^' so it only replaces the entries that start 56?

Thanks for your replies
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this code:

Code:
Sub ChangeNumbers()

    Dim TelRange As Range
    Dim Cell As Range
    
    'Set range containing telephone numbers
    Set TelRange = Worksheets("Sheet1").UsedRange.SpecialCells(xlCellTypeConstants, xlNumbers + xlTextValues)   'Amend accordingly
    
    For Each Cell In TelRange
        With Cell
            If Left(.Value, 2) = 56 Then
                .Value = 8523925 & .Value
            End If
        End With
    Next Cell

End Sub
 
Upvote 0
This works quite well, but I noticed that some fields in the other columns (B-F) are also changed and that is not required
Can I limit the range to the A column only?
 
Upvote 0
This works quite well, but I noticed that some fields in the other columns (B-F) are also changed and that is not required
Can I limit the range to the A column only?


Yes, to set the range to column A only, change the following line...

From:

Set TelRange = Worksheets("Sheet1").UsedRange.SpecialCells(xlCellTypeConstants, xlNumbers + xlTextValues)

To:

Set TelRange = Worksheets("Sheet1").Columns("A").SpecialCells(xlCellTypeConstants, xlNumbers + xlTextValues)
 
Upvote 0
Fabulous, that is exactly doing what I was trying to achieve.

Many, many thanks!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,180
Members
452,615
Latest member
bogeys2birdies

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