+ Reply to Thread
Results 1 to 4 of 4

Adding column to an array

Hybrid View

ChipsSlave Adding column to an array 07-20-2017, 02:07 PM
AB33 Re: Adding column to an array 07-20-2017, 04:22 PM
ChipsSlave The website contains table... 07-20-2017, 04:35 PM
AB33 Re: Adding column to an array 07-21-2017, 05:22 AM
  1. #1
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131

    Adding column to an array

    Hello all,

    I have got a VBA code which pulls data from a table on website. This data you can find on sheet called Sheet1. But my desired table i want it to look like is visualised in sheet called Desired. Would it be possible to add two variables to an array from website? The variables I want to add are nowDate, nowTime, cat, website.

    Sub DolphinFitness()
     Dim oHtml As HTMLDocument
     Dim oElement As Object
     Dim a, x, i As Long, ii As Long
     Dim nowDate, nowTime As Date
     Dim cat, website As String
     
     nowDate = Format(Now(), "dd/mm/yyyy", vbSunday)
     nowTime = Format(Now(), "hh:mm", vbSunday)
     website = "DolphinFitness"
     cat = "protein-isolate"
     
     'Needs a Reference.
     'Go to Tools > Reference > Search for Microsoft HTML Object Library > tick the checkbox > OK
     Set oHtml = New HTMLDocument
     
     With CreateObject("WINHTTP.WinHTTPRequest.5.1")
        .Open "GET", "http://www.dolphinfitness.co.uk/en/protein-isolate", False
        .send
        oHtml.body.innerHTML = .responseText
        Debug.Print
     End With
    
     ReDim a(1 To 100000, 1 To 5)
     For Each oElement In oHtml.getElementsByClassName("snapshot")
        i = i + 1
        x = Split(oElement.outerText, vbCr)
        For ii = 1 To UBound(x)
            a(i, ii) = Trim$(x(ii))
        Next
        
     Next oElement
     Sheets("Sheet1").Cells(2, 1).Resize(i, 5) = a
     
     '' remove line breaks
         Dim MyRange As Range
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
     
        For Each MyRange In ActiveSheet.UsedRange
            If 0 < InStr(MyRange, Chr(10)) Then
                MyRange = Replace(MyRange, Chr(10), "")
            End If
        Next
        
    '' remove sterling sign
        ActiveSheet.Range("C:C").Replace What:="£", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False
     
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Adding column to an array

    Where are the variables in the URL "http://www.dolphinfitness.co.uk/en/protein-isolate", False?
    I could not find them in the website either.
    Where exactly do you see these values in the web page? I can only see a table with 3 columns? Could you attach the URL where these data are found?

  3. #3
    Forum Contributor
    Join Date
    09-21-2016
    Location
    UK
    MS-Off Ver
    2016
    Posts
    131
    The website contains table only three columns. I want to add other 4 custom variables defined by user or the time when the macro is ran so i could keep a history on price changes. Does that make a sense?

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Adding column to an array

    To increase the size of the array or columns is not an issue, but the value of the category column appears to change (As per the desired output), so I do not know how you are going to get these values?
    There are many ways of adjusting the array. Quick and dirty fix which does not require looping, but inflexible is:

    ReDim a(1 To 100000, 1 To 8)
     For Each oElement In oHtml.getElementsByClassName("snapshot")
        i = i + 1
        x = Split(oElement.outerText, vbCr)
            a(i, 1) = nowDate
            a(i, 2) = nowTime
            a(i, 3) = website
            a(i, 4) = cat
            a(i, 5) = Trim$(x(1))
            a(i, 6) = Trim$(x(2))
            a(i, 7) = Trim$(x(3))
        
        
     Next oElement
     Sheets("Sheet1").Cells(2, 1).Resize(i, UBound(a, 2)) = a
    Another option:

     ReDim a(1 To 100000, 1 To 8)
     For Each oElement In oHtml.getElementsByClassName("snapshot")
        i = i + 1
        x = Split(oElement.outerText, vbCr)
        
        For ii = 1 To UBound(x)
            a(i, 1) = nowDate
            a(i, 2) = nowTime
            a(i, 3) = website
            a(i, 4) = cat
            a(i, ii + 4) = Trim$(x(ii))
        Next
        
     Next oElement
     Sheets("Sheet1").Cells(2, 1).Resize(i, UBound(a, 2)) = a

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Finding a column via header search and then adding everything in it into an array.
    By Scoobster_doo in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-22-2017, 09:58 AM
  2. Adding Column to Table with Array Formula
    By eliasc in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-10-2016, 03:48 PM
  3. [SOLVED] Adding another column to existing table with array formula
    By eliasc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-08-2016, 09:00 AM
  4. [SOLVED] 2 Column Lookup with 6 Column array Col index number is Varies Within Entire Array
    By Weasyb in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 08-05-2015, 12:54 PM
  5. [SOLVED] Adding to a new variable array from an existing array
    By Aussiexile in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-17-2014, 12:08 AM
  6. [SOLVED] Adding an extra column in a 2D array
    By j_Southern in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-24-2014, 07:27 AM
  7. Adding an array of Dates to an Array of Times.
    By cummins in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-25-2011, 09:12 PM

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