├── Module.bas └── README.md /Module.bas: -------------------------------------------------------------------------------- 1 | Attribute VB_Name = "Module1" 2 | ' To learn more about getElements visit: 3 | ' http://automatetheweb.net/vba-getelementsbytagname-method/ 4 | ' 5 | ' Modules to add in Tools/References: 6 | ' Microsoft Internet Controls 7 | ' Microsoft HTML Object Library 8 | Sub way() 9 | Dim i As Integer 10 | Dim o As Object 11 | 12 | 'On Error GoTo handling 13 | 'Continue to next line on error 14 | On Error Resume Next 15 | 'Declare a new browser instance 16 | Dim ie As InternetExplorer: Set ie = New InternetExplorer 17 | 18 | Dim url As String: url = "https://www.w3schools.com/html/html_tables.asp" 19 | 'Ctrl+G to open Immediate window 20 | Debug.Print url 21 | 22 | 'IE running in background, set to True to make ie visible 23 | ie.Visible = False 24 | 'Navigating to assigned webpage 25 | ie.navigate (url) 26 | 27 | Application.StatusBar = "Loading ..." 28 | 'Waiting for webpage to load. 29 | Do While ie.Busy = True Or ie.readyState <> 4: Loop 30 | 31 | 'Evaluating 'tr' elements data in the 'table' with id 'customers' 32 | For Each o In ie.document.getElementById("customers").getElementsByTagName("tr") 33 | i = i + 1 34 | 'In the table each (table row) element has 3 data/children 35 | 'Getting data content and assign value to specified cells. 36 | Sheets("Sheet1").Range("A" & i).Value = o.Children(0).textContent 37 | Sheets("Sheet1").Range("B" & i).Value = o.Children(1).textContent 38 | Sheets("Sheet1").Range("C" & i).Value = o.Children(2).textContent 39 | Next 40 | 41 | 'Clean-up dimensions 42 | Set o = Nothing 43 | Set ie = Nothing 44 | Call clear_all 45 | Application.StatusBar = "" 46 | 47 | 'Optional handling method 48 | 'handling: 49 | 'MsgBox "Error on InternetExplorer!" 50 | End Sub 51 | 'Subroutine function that cleans browsing history 52 | Sub clear_all() 53 | Shell "RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 255" 54 | End Sub 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## vba-webdata 2 | 3 | Scraping data from a webpage with getElements using Visual Basic for Applications. 4 | 5 | ### Enable developer mode on Excel 6 | 7 | - in Excel click File tab and goto Options 8 | - in Options, navigate to Customize Ribbon 9 | - under Main tabs, ensure that the checkbox for Developer is in check 10 | 11 | ### Importing VBA module 12 | 13 | - in Developer tab click Visual Basic to open the editor 14 | - `Right-click` on Project-VBAProject Window and select `Import File`
15 | - navigate the `.bas` file to be imported
16 | 17 | ### Create VBA module 18 | 19 | - goto Visual Basic Editor
20 | - `Right-click` on Project-VBAProject Window and select `Insert` → `Module` from the context menu
21 | - start coding :heart:
22 | - to run the code: simply press `F5` to execute the whole code or `F8` to run it by line 23 | 24 | 25 | --------------------------------------------------------------------------------