Remove characters in Excel cell using VBA macro

Kristolykid Posted messages 3 Status Member -  
Kristolykid Posted messages 3 Status Member -
Hello

I work with files that contain a list of phone numbers starting with:

tel:+33
tel:+262
tel:+590
tel:+594
tel:+596

I would like to create a macro that allows me to automatically remove these prefixes and keep only the phone number that appears after. (Knowing that I have to perform this operation for each new file since it's not necessarily the same people calling).

Thank you in advance for any help you can provide.

3 answers

  1. via55 Posted messages 14391 Registration date   Status Member Last intervention   2 759
     
    Hello

    It seems to me that a macro of this kind would be suitable:
    Sub telephone() Dim lig As Long lig = Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row 'last filled row of the 1st column A ADAPT for another column For Each cell In Range("A2:A" & lig) 'loop through all cells in the range from A2 to the last filled row A ADAPT for another column If Left(cell.Value, 5) = "tel:+" Then cell.Value = Right(cell.Value, 9) ' if the first 5 characters are tel:+ keep only the 9 characters to the right Next End Sub


    Best regards
    Via

    --
    "Imagination is more important than knowledge."    A. Einstein
    1
    1. Kristolykid Posted messages 3 Status Member
       
      Hello,

      Perfect, it works perfectly, thank you very much for your help.

      Best regards
      0
  2. Kristolykid Posted messages 3 Status Member
     
    Hello again,

    A little additional information to my previous message, a colleague suggested that I start from the end of the phone number and remove all characters that appear before the first displayed digit, which is the 9th from the right:

    For example: tel:+33123456789 (I remove the characters before 1).

    Indeed, it would be very easy to implement, but I also have lines where the phone number is replaced by an email address.

    So I need to be able to put a condition in my query not to truncate email addresses (I thought of a condition that if the cell contains "tel+" we remove it; otherwise, we leave it as is, but I don't know how to do that).

    Thank you very much for your help.
    0
  3. ccm81 Posted messages 11033 Status Member 2 434
     
    Hello

    Can you send a dummy file with all possible cases on cjoint.com and attach the obtained link in your next message? Don't forget to add explanations and examples of expected results
    1) Go to https://www.cjoint.com/
    2) Click on [Browse] to select your file
    3) Scroll down to the bottom of the page and click on [Create Cjoint link]
    4) After a few seconds, the second page will display the link
    in blue underlined; select it and click "Copy"
    5) Return to your discussion on CCM, and in your reply message, click "Paste".

    Best regards
    0