Find automatically text in url and add information to excel

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this. It has no error handling or annotation.

Code:
Option Explicit
Sub main()

scrape_youtube


End Sub
Sub scrape_youtube()

'
Dim IE As Object
Dim lastrow As Integer

On Error Resume Next

    Set IE = GetObject("InternetExplorer.Application")
If IE Is Nothing Then Set IE = CreateObject("INTERNETEXPLORER.APPLICATION")

Get_Info_From_Web IE, ThisWorkbook.Sheets("Sheet1").Range("A1")

extract_def

    IE.Quit

End Sub
Sub Get_Info_From_Web(ByRef IE As Object, ByVal lookup_word)
    
    With IE
        .Navigate lookup_word
        IE.Visible = True

        Do Until .ReadyState = 4: DoEvents: Loop
    End With
DoEvents

With IE.Document.getElementsByTagName("Body")(0)
    
    .Focus

End With

    IE.ExecWB 17, 0 '// SelectAll
    IE.ExecWB 12, 2 '// Copy selection
    ThisWorkbook.Sheets("Sheet2").Range("A1").Select
    ThisWorkbook.Sheets("Sheet2").PasteSpecial link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True

    IE.Quit

End Sub

Sub extract_def()

 Const Views = " views"
 Const PublishedOn = "Published on "

Dim rng As Range
Dim ViewsRow As Integer
Dim PublishedOnRow As Integer
Dim lastrow As Integer

PublishedOnRow = 0
ViewsRow = 0

With Sheets("Sheet2")

lastrow = .Range("A55555").End(xlUp).Row

Set rng = .Range("A:A").Find(What:=PublishedOn, LookIn:=xlValues, lookat:=xlPart, MatchCase:=False)

If Not rng Is Nothing Then
PublishedOnRow = rng.Row
Else
MsgBox ("Cannot find published date.")

End If

Set rng = .Range("A:A").Find(What:=Views, LookIn:=xlValues, after:=Range("A1"), lookat:=xlPart, MatchCase:=False)

If Not rng Is Nothing Then
ViewsRow = rng.Row

Else

MsgBox ("Cannot find No. of views")
Exit Sub
End If

.Range("A" & PublishedOnRow).Copy Destination:=ThisWorkbook.Sheets("Sheet1").Range("C1")
.Range("A" & ViewsRow).Copy Destination:=ThisWorkbook.Sheets("Sheet1").Range("B1")

End With

ThisWorkbook.Sheets("Sheet1").Range("B1") = Left(ThisWorkbook.Sheets("Sheet1").Range("B1"), InStr(ThisWorkbook.Sheets("Sheet1").Range("B1"), " views") - 1)
ThisWorkbook.Sheets("Sheet1").Range("C1") = Right(ThisWorkbook.Sheets("Sheet1").Range("C1"), Len(ThisWorkbook.Sheets("Sheet1").Range("C1")) - 13)
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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