1

I am new in Excel VBA/Macro

I need to grab the specific part of the page, not the full page. The bellow code works in complete page, but don't need the all parts of the page.

Sub GrabOutStandingTable()

With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://dsebd.org/displayCompany.php?name=ABBANK", Destination:=Range( _
    "$A$1"))
    .CommandType = 0
    .Name = "displayCompany.php?name=ABBANK"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingNone
    .WebTables = """company"""
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With
Sheets.Add After:=ActiveSheet
End Sub

The table part headed as "Other Information of the Company" is in lower part of the page, which is that I am talking about. The macro should extract this part.

Alex Knauth
  • 8,133
  • 2
  • 16
  • 31
user176705
  • 13
  • 6

2 Answers2

1

XHR request:

You could do a much faster browserless XHR request and simply target the table of interest, which is at position 23, if you collect elements by their (non-unique) company id.

I use querySelectorAll method to grab the matching nodes and then extract the table at index 23.

Note the additional sponsor info that is displayed in then code output.


Webpage view:

page


Sample code output:

Sheet view


Code:

Option Explicit
Public Sub GetTable()
    Dim sResponse As String, hTable As Object, HTML As New HTMLDocument
    Application.ScreenUpdating = False
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://dsebd.org/displayCompany.php?name=ABBANK", False
        .send
        sResponse = StrConv(.responseBody, vbUnicode)
    End With

    sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))
        With HTML
            .body.innerHTML = sResponse
            Set hTable = .querySelectorAll("#company")(23)
        End With
       WriteTable hTable
       Application.ScreenUpdating = True
End Sub

Public Sub WriteTable(ByVal hTable As HTMLTable, Optional ByVal startRow As Long = 1, Optional ByVal ws As Worksheet)

    If ws Is Nothing Then Set ws = ActiveSheet

    Dim tSection As Object, tRow As Object, tCell As Object, tr As Object, td As Object, R As Long, C As Long, tBody As Object
    R = startRow
    With ws
        Dim headers As Object, header As Object, columnCounter As Long
        Set headers = hTable.getElementsByTagName("th")
        For Each header In headers
            columnCounter = columnCounter + 1
            .Cells(startRow, columnCounter) = header.innerText
        Next header
        startRow = startRow + 1
        Set tBody = hTable.getElementsByTagName("tbody")
        For Each tSection In tBody               'HTMLTableSection
            Set tRow = tSection.getElementsByTagName("tr") 'HTMLTableRow
            For Each tr In tRow
                R = R + 1
                Set tCell = tr.getElementsByTagName("td")
                C = 1
                For Each td In tCell             'DispHTMLElementCollection
                    .Cells(R, C).Value = td.innerText 'HTMLTableCell
                    C = C + 1
                Next td
            Next tr
        Next tSection
    End With
End Sub

References:

VBE > Tools > References > HTML Object Library

QHarr
  • 83,427
  • 12
  • 54
  • 101
0

The old Data > From Web won't handle this due to the way that website is structured. The data you need is nested deeply into other tables and consists of several tables.

Suggest using Power Query instead (no VBA required). Here is how to use Power Query in XL2013 for this. Use Excel's ribbon and find tab POWER QUERY.

  1. Use menu option: POWER QUERY > From Web
  2. From Web dialog displays. Enter your URL.
  3. Click OK
  4. The data you need is in Table 30. Find and click it, then click Load.

If we have XL2016 (office 365) we already have Power Query. If we have XL2010 or XL2013 we can download it from: https://www.microsoft.com/en-us/download/details.aspx?id=39379&CorrelationId=1441491e-917e-43de-8d6a-21f98287c3c2

  • any other way please..... because if I do it manually I have to browse 300+ pages to collect the same structured data (the table) for 300+ company profile. So need to automate the procedure too.... in the url "name=ABBANK" this is the only variable for the link, if I just change the string "ABBANK" to another specific. it should load the another company profile In other word actually I need to collect the same structured data from the 300+ company profile. So, how could I do it?? – user176705 Oct 27 '16 at 19:07
  • Another thing is, I cant install PowerQuery tool till now. It need InternetExplorer 9 or Higher, my win7 system not support "EIE11_EN-US_WOL_WIN7.EXE" :( – user176705 Oct 27 '16 at 19:14
  • Yep, I have exactly what you need. Below is a link that explains it . I'll work up a prototype for you and post it here in a bit. – Craig Hatmaker Oct 27 '16 at 19:38
  • Here is a link to a prototype of what I think you need. Enter the bank names in the first column then use the toolbar's Load/Refresh Table to load all banks. Use toolbar's Load/Refresh Row to load just one. – Craig Hatmaker Oct 27 '16 at 20:07
  • https://dl.dropboxusercontent.com/u/13737137/Projects/WWW/wwwBanks.xlsm – Craig Hatmaker Oct 27 '16 at 20:07
  • You will also need this zip. Extract it to the same directory where you save wwwBanks.xlsm. The zip contains icons used by wwwBanks.xlsm https://dl.dropboxusercontent.com/u/13737137/Projects/WWW/WWWHelper.zip – Craig Hatmaker Oct 27 '16 at 20:11
  • Thanks for your efforts.... manual procedure! but is it possible to recall the Public share % to another worksheet for analysis?? – user176705 Oct 27 '16 at 20:50
  • Manual? Just key bank's IDs then click load table. Want an update a week later? Click load again - there's nothing else to do. Once downloaded the table can be used just like any other XL table. LInk via external formulas, MS Query, Power Query, Power BI ... – Craig Hatmaker Oct 28 '16 at 18:39
  • Hey,@CraigHatmaker I was looking for similar spreadsheet..The links dead,can you please post the link again ?? – Pramod Pandit Dec 01 '20 at 15:52
  • wwwHelper is no longer available. It relied on IE which MS, essentially, disabled in Win10. – Craig Hatmaker Mar 24 '21 at 19:57