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§orid=&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
Bookmarks