Clean up datas with VBA and regex

Mercury74

New Member
Joined
Jul 13, 2014
Messages
10
Hi,

I want to clean a worksheet with multiple lists of urls. Those urls have some strings at the beginning and at the end.
For exemple:

sdab588dddhttp://www.url1.com/page.php&sa=qsd554/ds4d
dab588dddhttp://www.url2.com/index.html&sa=fffsdf?result=4
dab588dddhttps://www.urlx.com/&sa=dfdf/5454?fdfd&id=24
...

Black = url to keep
Red = strings to remove


Important :

  • Every strings to remove after urls always start with &sa=
  • Some urls start with https, so take care!
  • I want a "click and cleanup" solution with VBA, not a formula
  • The number of columns is variable
  • Do not create any new columns with cleaned urls, but simply erase unwanted strings.


Thanks!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I don't use regex, but this will work, none-the-less:

Code:
Sub CleanURLs()

Dim cl      As Range
Dim stVal   As String
Dim iLeft   As Long
Dim iRight  As Long


For Each cl In Selection

iLeft = InStr(cl.Value, "http")
iRight = InStr(cl.Value, "&sa")

stVal = Mid(cl.Value, iLeft, iRight - iLeft)

cl.Value = stVal

Next cl

End Sub

Select the range to clean, and then run the macro.
 
Upvote 0
Here is another macro that should work for you...
Code:
Sub CleanURLs()
  Cells.Replace "&sa=*", "", xlPart, , False
  Cells.Replace "*http", "http", xlPart, , False
End Sub
 
Upvote 0
Code:
Sub M_snb()
    Cells.Replace "*http", "http"
    Cells.Replace "&*", ""
End Sub
 
Upvote 0
Code:
Sub M_snb()
    Cells.Replace "*http", "http"
    Cells.Replace "&*", ""
End Sub
I would suggest that, at minimum, you add the third argument xlPart (see my code in Message #3), otherwise your code will fail to work if the last time Find or Replace was used (either manually or though other VB code), the "Match entire cell contents" option box was checked or specified.
 
Upvote 0
In that case you only need it once:

Code:
Sub M_snb()
    Columns(1).Replace "*http", "http", 2
    Columns(1).Replace "&*", ""
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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