Extract text from URL

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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)
 
Upvote 0
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.
 
Upvote 0
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*"
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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