Trim a URL

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,064
Office Version
  1. 2016
Platform
  1. Windows
I want to trim a url with vba code so the url will give me the domain name ONLY, from the url below I want the word google
1585058155825.png


Urls will be in A1 down as the list is dynamic. I will have a command button on on a userform, url will be in fourth sheet called "Sheet4"
  • This is in B1 down =RIGHT(A1,LEN(A1)-12) and gives me, google.com/search?q=google+news&oq=google+&aqs=chrome.0.69i59j0l5j69i57j0.8860j0j8&sourceid=chrome&ie=UTF-8
  • This is in C1 down =LEFT(B1,FIND(".",B1)-1) and this Give me google

However,
The first formula only works on https and not http as I have to -12 and the second formual looks for the first "." and removes every thing after it and -1
Also the formual is in two columns, so three columns are used A,B,C, where as I wanted it to change all the results in column A and then convert to values and not show as formulas,

Thanks having a look
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try:

=TRIM(RIGHT(SUBSTITUTE(LEFT(A1,MIN(SEARCH({".com",".org",".edu",".gov"},A1&".com.org.edu.gov"))-1),".",REPT(" ",999)),999))

assuming you only have the 4 basic identifiers.
 
Upvote 0
Eric W

Thanks for your reply, the basic identifiers could be any thing, .co.uk .uk, .ca .au and so on will this be an issue?

I tried this and got an error on the second line
VBA Code:
Private Sub CommandButton1_Click()
    Worksheets("Sheet4").Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=RIGHT(A1,LEN(A1)-12)"
    Worksheets("Sheet4").Range("C1:C" & Range("B" & Rows.Count).End(xlUp).Row).Formula = "=LEFT(B1,FIND(""."",B1)-1)"""
End Sub

1585061139720.png
 
Upvote 0
Hmm, I'd forgotten the international identifiers. Try this instead:

=TRIM(MID(SUBSTITUTE(A1,".",REPT(" ",999)),20,999))

and if you want to encode that in VBA try:

Rich (BB code):
Worksheets("Sheet4").Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=TRIM(MID(SUBSTITUTE(A1,""."",REPT("" "",999)),20,999))"

Note that the interior double quotes must be doubled so VBA can recognize that they're not the enclosing quotes.
 
Upvote 0
The vba is the way I wish to go, Almost there Eric could you take a look at this as you can see it has chopped parts of the characters off

These are showing as http but will be a mix of http and https
Column B should read as such
arnoldclark
van-deals
withamgarage
browningsuzuki
1585062944917.png
 
Upvote 0
Oops, sorry, change the 20 to 999. Also note that the formula finds what's between the first 2 periods. So for davidbaileygarage.co.uk you'll get davidbaileygarage, not davidbaileygarage.co, but I think that's what you want.
 
Upvote 0
Eric W

Thanks, that is super duper. Thank god you replied, I was about to post my half hearted work around. I will save myself the embarrassment.
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

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