Macro to trim text in cell with condition

V51773

New Member
Joined
Feb 24, 2021
Messages
22
Office Version
  1. 2019
Platform
  1. Windows
Hello All,

Im looking into learning how to macro all the cells say in column A to remove the following from a website entry, example http://www.PubHorn01.com.ar or https://www.PubHorn01.com.ar or www.PubHorn01.com, http://www.PubHorn01.com/adasdasd, now i've tried using find and replace then recording it as a macro, but my problem is the data is not constant, i just want my end result to be .PubHorn01.com.ar, but again that raises another issue some website just ends with .com , some website has .com.ph , au , ar etc . so the condition must be itll keep both .com.xx and .com. and it would also ignore not make any changes to correct format entries.

from this data

www.PubHorn01.com.xx/
www.PubHorn01.tk
www.PubHorn01.info
PubHorn01.info

expected results is

PubHorn01.com
PubHorn01.com.ar
PubHorn01.com.xx
PubHorn01.tk
PubHorn01.info
PubHorn01.info
 
Last edited:
Hello Mohadin,

Please see attachment, higlighted in yellow for bug outputs.

Thank you for your help.
 

Attachments

  • a.png
    a.png
    30.8 KB · Views: 3
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I wasn't aware that this was going to be an evolving and growing dataset
 
Upvote 0
Hello Dan,

Thank you for your reply, for some reason the above code is giving me a #value error. also i have one more condition needed.
entry
www.john.blogspot.com

output
john.blogspot.com

Thank you,
Mike
It's giving you an error, because I wrote it on the assumption that when you said
you meant
 
Upvote 0
Hello Dan,

Sorry about that, I've went on ahead and used Mohadins solution, and just added a counter argument when it fails, as it seems the constant value i can base on there are the https: and www, apologies for the hassle, as I've realized the latter requests while i was fiddling with the codes ya'll generously supplied, I was about to edit the main thread but it wouldnt allow me anymore. Thanks again for your help and sorry for the hassle.

regards,
Mike
 
Upvote 0
For the future, I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with. ;)

Here is another code you could test.

VBA Code:
Sub WebsiteInfo()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "((https?://)?(www\.)?)"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
        a(i, 1) = Left(RX.Replace(a(i, 1), ""), InStr(1, RX.Replace(a(i, 1), "") & "/", "/") - 1)
    Next i
    .Offset(, 1).Value = a
  End With
End Sub

My sample data and results:

V51773.xlsm
AB
1
2http://www.PubHorn01.com/asasasasasPubHorn01.com
3http://PubHorn01.com/asasasasasPubHorn01.com
4http://www.PubHorn01.com/asasasasas/asasasaPubHorn01.com
5www.PubHorn01.com.xx.yy/awPubHorn01.com.xx.yy
6PubHorn01.infoPubHorn01.info
7john.blogspot.comjohn.blogspot.com
8
9http://z--z.com/z--z.com
10www.man-whole.comman-whole.com
11http://yongqing.is-programmer.comyongqing.is-programmer.com
Sheet1
 
Upvote 0
Solution
Hello Peter,

Thank you for your help, this is exactly what i was looking for, just one question just so i can understand how the programming works, apologies for the hassle on the sample data.

VBA Code:
 RX.Pattern = "((https?://)?(www\.)?)"  <-this is where you remove the http and www.

my question is if say i would add, ftp or rtp, or any other name that would be invented in the future , do i add it like this?

VBA Code:
RX.Pattern = "((https?://)(ftp?://)?(www\.)?)"

sorry for the dumb question, i really want to understand how this argument works, how did the code know it should ignore "/" sorry for the long question my understanding of this is still pretty vague. i merely code with tons of nested ifs ahhaha

also if theres a book or documentation you can point me through so i can read more on this , thank you very much.


Thank you so much for your help.

regards

Mike
 
Upvote 0
Hello Peter,

Disregard the last inquiry, i played around with the code and got it to work.

VBA Code:
  RX.Pattern = "((https?://)?(ftp\.)?(ww3\.)?(www\.)?)" <-- this is what i got

Thank you for your help, I was able to understand the gist of it all, just need to read more on regrex as this is my first time encountering it <3, thanks again and if there are books or documentation you can point me through to study that would be great,

Hello Dan,

Thank you for your help as well..


Hello Mohadin,

Thank you for your assistance too, ill compare your code with Peter and see where i can make out what their differences are.


regards

Mike
 
Upvote 0
Disregard the last inquiry, i played around with the code and got it to work.

VBA Code:
RX.Pattern = "((https?://)?(ftp\.)?(ww3\.)?(www\.)?)" <-- this is what i got
Thank you for your help, I was able to understand the gist of it all, ...
You're welcome. :)
.. and that pattern looks pretty good to me. (y)


.. if there are books or documentation you can point me through to study that would be great,
Finding info for regular expressions used with vba I found somewhat difficult to find.
I did purchase this book and it was some help. It is now out of print but there is a further link on that page to a replacement book which I assume is similar.
There are also some websites if you search for them. A few rom my list, in no particular order
Excel Regex Tutorial (Regular Expressions) - Analyst Cave
Regular expressions - An introduction
Zytrax Tech Stuff - Regular Expressions - A Gentle User Guide and Tutorial (includes a testing capability)

However, I did am still doing most of my learning of regular expressions in this forum. :)
 
Upvote 0
Hello Peter,

Thank you again for your help and the study/review material, have a wonderful day chief.

regards,
Mike
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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