Results 1 to 8 of 8

Reduce code size to get Http Request from server

Threaded View

  1. #1
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Reduce code size to get Http Request from server

    Hi VBA experts

    My code works well, I'd like to tidy it up, but I am unsure how to do go about doing that.
    As you can see it's all over the place and some are repeated often
    Please may I have your advice on this

    Very much appreciated!


    Private Function HttpGetRequest(Url As String) As String
    
        Dim xmlReq As ServerXMLHTTP60
    
    
            Set xmlReq = New ServerXMLHTTP60
            xmlReq.Open "GET", Url
            xmlReq.setRequestHeader "User-Agent", "Excel 2013 :)"
            xmlReq.send
    
        If xmlReq.Status <> 200 Then MsgBox "Error occured: " & xmlReq.statusText: Exit Function
    
    HttpGetRequest = xmlReq.responseText
    End Function
    
    Sub Test()
    Dim servResp As String
    Dim htmlDoc As HTMLDocument, myTable As IHTMLTable, myTableRow As IHTMLTableRow, myCell As IHTMLTableCell
    Dim i As Long, colSpan As Integer, rowSpan As Integer
    Dim t As Integer, r As Integer, c As Integer
    
        Cells.Select
        Selection.Delete
            
            servResp = HttpGetRequest("http://www.hl.co.uk/funds/fund-discounts,-prices--and--factsheets/search-results?investment=&companyid=218&sectorid=&sort=az&tab=prices")
        
        Set htmlDoc = New HTMLDocument
        
        htmlDoc.body.innerHTML = servResp
        
        Set myTable = htmlDoc.all.tags("TABLE").Item(0)
                
       For Each myTableRow In myTable.Rows
            
            For Each myCell In myTableRow.Cells
                               colSpan = myCell.getAttribute("colspan")
                               rowSpan = myCell.getAttribute("rowspan")
                        
    If myCell.cellIndex = 0 And myTableRow.RowIndex <> 1 And myCell.innerText <> "Unbundled funds" And myCell.innerText <> "Inclusive funds" Then
        Cells(r + 1, 1).Select
        ActiveCell = myCell.innerText
    Else
            If myCell.cellIndex < 4 And myTableRow.RowIndex <> 1 And myCell.innerText <> "Unbundled funds" And myCell.innerText <> "Inclusive funds" And myCell.innerText <> "" Then
                ActiveCell.Offset(, c).Select
                ActiveCell.Resize(rowSpan, colSpan).Select
                Selection.Merge
                ActiveCell = myCell.innerText
            Else
                    If myTableRow.RowIndex = 1 And myCell.cellIndex = 0 And myCell.innerText <> "Unbundled funds" And myCell.innerText <> "Inclusive funds" Then
                            Cells(2, 2).Select
                            ActiveCell.Resize(rowSpan, colSpan).Select
                            Selection.Merge
                            ActiveCell = myCell.innerText
                    Else
                    If myCell.innerText = "Unbundled funds" Or myCell.innerText = "Inclusive funds" Then
                    r = r - 1
                    Else
                 If myCell.innerText = "" Then
                    Else
    
                    If myCell.cellIndex < 3 Then
                            ActiveCell.Offset(, c).Select
                            ActiveCell.Resize(rowSpan, colSpan).Select
                            Selection.Merge
                            ActiveCell = myCell.innerText
                      End If
                      End If
                      End If
                    End If
            End If
    End If
                c = 1
            Next myCell
            
            r = r + 1
    
        Next myTableRow
        
        Range("A1").Select
        Selection.Cut Destination:=Range("A2")
                            
        Columns("E:F").Select
        Selection.Delete
    
    
        
        Columns.AutoFit
        
            Range("A2:D2").Select
        Selection.AutoFilter
        Range("A3").Select
        ActiveWindow.FreezePanes = False
        ActiveWindow.FreezePanes = True
    End Sub
    Last edited by kaseyleigh; 01-12-2015 at 07:36 AM. Reason: Comply with Rule 7

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need a help to tidy up VBA code for auto scaling a chart.
    By Burt0 in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 09-10-2014, 08:37 PM
  2. [SOLVED] Code Amend: Tidy Up andDelete All Rows Where Cell Value Is Number
    By TextMonkey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-06-2014, 01:50 PM
  3. tidy up code produced by macro recorder
    By ADAMC in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-31-2007, 09:24 AM
  4. Goto misused: help to tidy Code
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-20-2005, 12:36 AM
  5. smart & tidy code for many checkBox_Change()
    By Fendic in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-14-2005, 09:52 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1