Remove all text except URLs

jjokol

Board Regular
Joined
Dec 21, 2008
Messages
213
How can I remove all the text from a spreadsheet except URLs? Another way to look at it would be removing all text except anything that begins www.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this:

Code:
Sub test2()
Dim oMatch, oMatches
Dim ws As Worksheet
Dim rngAll As Range, rngFound As Range
Dim strFirstAddress As String

Set rngFound = ActiveSheet.UsedRange.Find(What:="www.", LookIn:=xlValues, lookat:=xlPart, MatchCase:=False)

If Not rngFound Is Nothing Then
    'check if any cells contain "www.":
    Set rngAll = rngFound
    strFirstAddress = rngFound.Address
    'loop thru all instances of "www." in sheet to return all relevant cells:
    Do
        Set rngFound = ActiveSheet.Cells.FindNext(rngFound)
        If rngFound.Address <> strFirstAddress Then _
            Set rngAll = Application.Union(rngAll, rngFound)
    Loop While strFirstAddress <> rngFound.Address
Else
    MsgBox "No URLs found!"
    Exit Sub
End If

'now extract all URLs and append to new sheet:

Set ws = Worksheets.Add
ws.Range("A1") = "URL"

With CreateObject("vbscript.regexp")
    .Global = True
    .ignorecase = True
    .Pattern = "www\.[^ ]+"
    For Each rngFound In rngAll
        If .test(rngFound.Value) Then
            Set oMatches = .Execute(rngFound.Value)
            For Each oMatch In oMatches
                ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1).Value = oMatch
            Next oMatch
            Set oMatches = Nothing
        End If
    Next rngFound
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,253
Members
452,900
Latest member
LisaGo

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