Extract text from URL

Some videos you may like

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
1,707
Let's suppose your URLs are in column A and you wish to create that string in column B.
Try this macro:
Code:
Sub AnB()
Dim FixStr As String, I As Long, mySplit
Dim CRV, CExNum As String, cDash, oStr As String
'
FixStr = " A4 GLOSSY PHOTO PRINT ### *FREE P&P*"
'
For I = 1 To Cells(Rows.Count, "A").End(xlUp).Row                               'Scan each row
    CRV = UCase(Cells(I, "A").Value)
    If UCase(Left(CRV, 4)) = "HTTP" Then                                        'seams a Url
        mySplit = Split(CRV, "/", , vbTextCompare)                              'parse by /
        If UBound(mySplit) > 1 And Right(CRV, 4) = ".JPG" Then                  'enough items & jpg?
            cDash = InStrRev(mySplit(UBound(mySplit)), "-", , vbTextCompare)
            If cDash > 0 Then
                oStr = Replace(FixStr, "###", "#" & Replace(Mid(mySplit(UBound(mySplit)), cDash + 1), ".jpg", "", , , vbTextCompare), , , vbTextCompare)
                oStr = Left(mySplit(UBound(mySplit)), cDash - 1) & oStr
                Cells(I, "B").Value = oStr
            End If
        End If
    End If
Next I
End Sub
Insert the code in a Standard Module of your vba, for example in Module1; then, when the url list is ready, run Sub AnB:
-from Excel, the right worksheet selected; press Alt-F8; select AnB from the list of available macros and press Run

The Url list will be scanned, and the string shall be written in column B

Bye
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
44,634
Office Version
365
Platform
Windows
I have over 40,000 url ..
In that case I think you will find this considerably faster to process.
I have assumed the url's start in cell A2 also with results going in to column B

Rich (BB code):
Sub ExtractFromURL()
  Dim a As Variant, b As Variant
  Dim urlBits(1 To 2) As String
  Dim i As Long
  
  Const strBase As String = " A4 GLOSSY PHOTO PRINT #@ *FREE P&P*"
  
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    urlBits(2) = Replace(Mid(a(i, 1), InStrRev(a(i, 1), "-") + 1), ".jpg", "", 1, 1, 1)
    urlBits(1) = Replace(Mid(a(i, 1), InStrRev(a(i, 1), "/") + 1), "-" & urlBits(2) & ".jpg", "", 1, 1, 1)
    If IsNumeric(urlBits(2)) Then b(i, 1) = UCase(urlBits(1) & Replace(strBase, "@", urlBits(2)))
  Next i
  Range("B2").Resize(UBound(b)).Value = b
End Sub
 

Freddie Dogg

New Member
Joined
Oct 22, 2019
Messages
3
In that case I think you will find this considerably faster to process.
I have assumed the url's start in cell A2 also with results going in to column B

Rich (BB code):
Sub ExtractFromURL()
  Dim a As Variant, b As Variant
  Dim urlBits(1 To 2) As String
  Dim i As Long
  
  Const strBase As String = " A4 GLOSSY PHOTO PRINT #@ *FREE P&P*"
  
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    urlBits(2) = Replace(Mid(a(i, 1), InStrRev(a(i, 1), "-") + 1), ".jpg", "", 1, 1, 1)
    urlBits(1) = Replace(Mid(a(i, 1), InStrRev(a(i, 1), "/") + 1), "-" & urlBits(2) & ".jpg", "", 1, 1, 1)
    If IsNumeric(urlBits(2)) Then b(i, 1) = UCase(urlBits(1) & Replace(strBase, "@", urlBits(2)))
  Next i
  Range("B2").Resize(UBound(b)).Value = b
End Sub
This works fantastic, thank you. However just realised that there are some PNG images too, can you amend the code please?
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
44,634
Office Version
365
Platform
Windows
However just realised that there are some PNG images too, can you amend the code please?
Does this modification work for you?

Code:
<del>urlBits(2) = Replace(Mid(a(i, 1), InStrRev(a(i, 1), "-") + 1), ".jpg", "", 1, 1, 1)</del>
urlBits(2) = Split(Mid(a(i, 1), InStrRev(a(i, 1), "-") + 1), ".")(0)
 

Freddie Dogg

New Member
Joined
Oct 22, 2019
Messages
3
Hi Peter, I am getting a Run-time error '9': Subscript out of range when I replace with modification above.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
44,634
Office Version
365
Platform
Windows
Hi Peter, I am getting a Run-time error '9': Subscript out of range when I replace with modification above.
When you get the error, click Debug and hover your cursor over the 'a' in a(i,1) in the yellow line.
What is the url displayed?

Alternatively, hover over the 'i' and if that was, say 10, look at row 11 in your sheet to check that url.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,907
Office Version
2010
Platform
Windows
There is a formula solution possible if you are interested in one...
Code:
=SUBSTITUTE(SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(LEFT(UPPER(A1),LEN(A1)-4),"/",REPT(" ",300)),300)),"-"," ",1),"-"," A4 GLOSSY PHOTO PRINT #")&" *FREE P&P*"
 

Watch MrExcel Video

Forum statistics

Threads
1,099,045
Messages
5,466,219
Members
406,473
Latest member
Dunno123

This Week's Hot Topics

Top