multiple live feed / data via excel

raygem

New Member
Joined
Jun 29, 2009
Messages
13
Hi All

I have set an excel spreadsheet set up to update live data from a URL every 5 minutes. However I am wanting to do try and save a bit of time if possible.

Basically I am monitoring horses bought on a game website. Each horse has the same URL except for the number at the end which goes up in 1 increments. The URL is already set up and when the horse is bought it changes from being blank to having the horses details etc.

At the minute I am able to autofill cells with each uinique URL using fill, then series menu. This is so I dont have to manually enter each URL, as hundreds may be bought each day so it would take too long to do this.

My question is this, am i able to have a automatic series linked to the 'data from web' function in data tab, instead of having to manually adjusting each URL for each individual horse.

Not sure if this is possible.

If anyone could help that would be great and if anyone needs me to explain it further just let me know.

Thanks
Gemma
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
It's not clear to me how you are getting the data and what you want to do, but a web query might work for this, if you're not using one already. Create the web query in VBA - use the macro recorder to get the code and then edit the Create_WebQuery subroutine below with the code generated. The query is then refreshed in a loop for each horse id/number, something like this:

Code:
Public Sub test()

    Dim QT As QueryTable
    Dim horseId As Long
    
    Set QT = Create_WebQuery(Worksheets("Sheet3"))
        
    If Not QT Is Nothing Then
    
        For horseId = 1 To 99
            QT.Connection = "URL;http://thewebsite.com/index.asp?horseId=" & horseId
            QT.Refresh BackgroundQuery:=False
            
            'Here, copy required data from web query sheet to your data sheet
        Next
    End If
    
End Sub

Private Function Create_WebQuery(querySheet As Worksheet) As QueryTable
    
    'Create web query.  Note that a URL is not specified in the Connection string, but instead is specified when querying
    'each horse URL
    
    Set Create_WebQuery = querySheet.QueryTables.Add(Connection:="URL;", Destination:=querySheet.Range("A1"))
     
    If Not Create_WebQuery Is Nothing Then
        With Create_WebQuery
            .Name = "Your query"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = False            'Wait for query to return before requesting next one (was True with Macro Recorder)
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlEntirePage
            .WebFormatting = xlWebFormattingNone
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            '.Refresh BackgroundQuery:=False    'Refresh when retrieving each horse URL, not here
        End With
    End If
    
End Function
 
Upvote 0
It's not clear to me how you are getting the data and what you want to do, but a web query might work for this, if you're not using one already. Create the web query in VBA - use the macro recorder to get the code and then edit the Create_WebQuery subroutine below with the code generated. The query is then refreshed in a loop for each horse id/number, something like this:

Code:
Public Sub test()
 
    Dim QT As QueryTable
    Dim horseId As Long
 
    Set QT = Create_WebQuery(Worksheets("Sheet3"))
 
    If Not QT Is Nothing Then
 
        For horseId = 1 To 99
            QT.Connection = "URL;http://thewebsite.com/index.asp?horseId=" & horseId
            QT.Refresh BackgroundQuery:=False
 
            'Here, copy required data from web query sheet to your data sheet
        Next
    End If
 
End Sub
 
Private Function Create_WebQuery(querySheet As Worksheet) As QueryTable
 
    'Create web query.  Note that a URL is not specified in the Connection string, but instead is specified when querying
    'each horse URL
 
    Set Create_WebQuery = querySheet.QueryTables.Add(Connection:="URL;", Destination:=querySheet.Range("A1"))
 
    If Not Create_WebQuery Is Nothing Then
        With Create_WebQuery
            .Name = "Your query"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = False            'Wait for query to return before requesting next one (was True with Macro Recorder)
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlEntirePage
            .WebFormatting = xlWebFormattingNone
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            '.Refresh BackgroundQuery:=False    'Refresh when retrieving each horse URL, not here
        End With
    End If
 
End Function



Thanks alot for the help John. To explain further I have a URL which is exactly the same for each horse except the number at the end. At the moment I am manually entering a web query in Excel through data; get external data; from web. I have not set a VBA up before. The data i am getting is to monitor how many horses are being bought each day and what grading the horses get, new owners details etc. I am planning once i get 100 horses details i will copy and paste them to another workbook and change the web query so i dont have thousands of pieces of data on one workbook.

Like I say i am unfamiliar with VBA and macro reader. I have managed to figure you press alt F11 to get to VBA page then I am able to add a module and add the code you have given me. What i need to know is where do i go from here? Do i only need to adjust the website in the code or other information too?

Also the bit that i get stuck on is 'use the macro recorder to get the code and then edit the Create_WebQuery subroutine below with the code generated'. So do i record before i input the web query via VBA then stop when i have completed this? And how do i get the new code generated?

Sorry if it is too many questions its just I really want to try and set this up if possible and at the same time learn new aspects of excel. i am able to guide myself around excel easy enough it is just implementing your suggestions which is hard for me.

If you dont have time to reply please dont worry i realise it is alot to ask.

Thanks
Gemma
 
Upvote 0
Do i only need to adjust the website in the code or other information too?
You need to change the website URL in the code (the http://thewebsite.com/index.asp?horseId= bit - the full URL with the number at the end omitted, as in my example) and the web query code between the With and End With in Create_WebQuery(). Your web query code will be different from my example, depending on the table(s) imported and the query formatting options chosen.

Also the bit that i get stuck on is 'use the macro recorder to get the code and then edit the Create_WebQuery subroutine below with the code generated'. So do i record before i input the web query via VBA then stop when i have completed this? And how do i get the new code generated?
Yes to the first question - start the macro recorder, manually create your web query as normal, then stop the recorder. The code generated will be in a new VBA module - press Alt+F11 to open the VB editor to see it.

It's easiest to help you if you post your unmodified web query code, exactly as generated by the macro recorder, and then I will incorporate that code into mine.
 
Upvote 0
Thanks for reply. Here is the unmodified code. I have manually added 3 horse details, startung with 6000000. i hope this is right, if not let me know.

Thanks again

Gemma

Sub Macro1()
'
' Macro1 Macro
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.digiturf.com/Betting/TransferHistory.asp?HorseID=606000", _
Destination:=Range("$A$1"))
.Name = "TransferHistory.asp?HorseID=606000"
.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 = "3"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Range("A14").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.digiturf.com/Betting/TransferHistory.asp?HorseID=606001", _
Destination:=Range("$A$14"))
.Name = "TransferHistory.asp?HorseID=606001"
.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 = "3"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Range("A27").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.digiturf.com/Betting/TransferHistory.asp?HorseID=606002", _
Destination:=Range("$A$27"))
.Name = "TransferHistory.asp?HorseID=606002"
.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 = "3"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
 
Upvote 0
This should be enough to get you started. It extracts horse data from the web query and tabulates it on Sheet1. Put this code in a new module in a new workbook and run the Extract_Horse_Data macro.

One refinement would be to choose the start and end HorseID rather than hardcoding them - maybe via user input with Inputbox or picked up from cell values somewhere.
Code:
Option Explicit

Public Sub Extract_Horse_Data()

    Dim baseURL As String, horseURL As String
    Dim webQuerySheet As Worksheet, horseDataSheet As Worksheet
    Dim i As Integer
    Dim horseQuery As QueryTable
    Dim horseID As Long
    Dim horseDataRow As Long
    
    Debug.Print Now; "Started"
    
    'Base URL with HorseID parameter omitted.  The parameter name and value is appended to the web query when requesting
    'each horse
    
    baseURL = "http://www.digiturf.com/Betting/TransferHistory.asp"
    
    'Sheet where web data for each horse is copied to
    
    Set horseDataSheet = Worksheets("Sheet1")
        
    'Sheet where web query is created
    
    Set webQuerySheet = Worksheets("Sheet3")
        
    'Set starting row on horse data sheet for copying horse web data to.  If A1 is empty the sheet is cleared and initialised
    'with column headers
    
    With horseDataSheet
        
        If .Range("A1").Value = "" Then
        
            'Initialise horse data sheet with column headers in row 1, and set starting row to row 2
        
            .UsedRange.ClearContents
            .Range("A1").Resize(1, 11).Value = Array("URL", "Owner Name", "Date Joined", "Horse Name", _
                    "Gr", "Cl Ra", "Age", "Sex", "Colour", "Birthday", "Bought")
            
            horseDataRow = 2
        
        Else
    
            'Set starting row to first empty row found in column A
    
            horseDataRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        
        End If
        
    End With

    'Delete all web queries on webQuerySheet
    
    webQuerySheet.UsedRange.ClearContents
    For i = webQuerySheet.QueryTables.Count To 1 Step -1
        Debug.Print Now; "Deleting query table " & webQuerySheet.QueryTables(i).Name
        webQuerySheet.QueryTables(i).Delete
    Next
    
    'Create the web query for retrieving horse data
    
    Set horseQuery = Create_WebQuery(webQuerySheet)

    If Not horseQuery Is Nothing Then
    
        'Request web data for each HorseID, copying details to next row on horse data sheet
        
        For horseID = 606000 To 606003
            horseURL = baseURL & "?HorseID=" & horseID
            Get_Horse_Data horseQuery, horseURL, horseDataSheet.Cells(horseDataRow, 1)
            horseDataRow = horseDataRow + 1
        Next
    
    End If
    
    Debug.Print Now; "Finished"
    
End Sub


Private Sub Get_Horse_Data(QT As QueryTable, sURL As String, copyToRange As Range)

    'Request web data for a specific horse.  If the horse exists its details are copied to the horse data sheet, otherwise
    'blank details are copied
    
    Dim savedErr As ErrObject
    
    QT.Connection = "URL;" & sURL
    
    On Error Resume Next        'Trap possible errors from refreshing web query
    
    QT.Refresh BackgroundQuery:=False
    
    If Err.Number = 0 Then

        'No error occurred - copy data from retrieved web data to data sheet
        
        On Error GoTo 0
        Debug.Print Now; sURL & " - Retrieved OK"
        
        With copyToRange
            .Offset(0, 0) = sURL
            
            'An owner name of "TRANSFER HISTORY" means the horse doesn't exist and all the details copied are blank
            
            If QT.Destination.Range("A5").Value <> "TRANSFER HISTORY" Then
                .Offset(0, 1) = QT.Destination.Range("A5").Value    'Owner name
            End If
            .Offset(0, 2) = QT.Destination.Range("D5").Value        'Date joined
            .Offset(0, 3) = QT.Destination.Range("A8").Value        'Horse name
            .Offset(0, 4) = QT.Destination.Range("D8").Value        'Gr
            .Offset(0, 5) = QT.Destination.Range("G8").Value        'Cl Ra
            .Offset(0, 6) = QT.Destination.Range("J8").Value        'Age
            .Offset(0, 7) = QT.Destination.Range("M8").Value        'Sex
            .Offset(0, 8) = QT.Destination.Range("N8").Value        'Colour
            .Offset(0, 9) = QT.Destination.Range("O8").Value        'Birthday
            .Offset(0, 10) = QT.Destination.Range("P8").Value       'Date bought
        End With
        
    Else

        'An unexpected error occurred - tell the user
        
        Set savedErr = Err
        On Error GoTo 0
        Debug.Print Now; sURL & " - Error " & savedErr.Number & " " & savedErr.Description

        MsgBox "Weq query URL: " & sURL & vbNewLine & _
            "Error number " & savedErr.Number & vbNewLine & _
            savedErr.Description, , "Web query error"

    End If

End Sub


Private Function Create_WebQuery(webQuerySheet As Worksheet) As QueryTable
    
    'Create web query.  Note that a URL is not specified in the Connection string, but instead is specified when querying
    'each horse
    
    Set Create_WebQuery = webQuerySheet.QueryTables.Add(Connection:="URL;", Destination:=webQuerySheet.Range("A1"))
     
    If Not Create_WebQuery Is Nothing Then
        With Create_WebQuery
            .Name = "TransferHistory.asp"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = False            'Wait for query to return before requesting next one (was True with Macro Recorder)
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingNone
            .WebTables = "3"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            '.Refresh BackgroundQuery:=False    'Refresh when retrieving each URL, not here
        End With
    Else
        MsgBox "Error creating web query" & vbNewLine & vbNewLine & _
            "Error number = " & Err.Number & vbNewLine & Err.Description
    End If
    
End Function
 
Upvote 0
:)John I cannot thank you enough it works perfectly. I would never in a million years have been able to come up with this, so thank you again.

Gemma
 
Upvote 0
To John W:

Thanks so much for your answer to Raygem's query, as it (or something very close to it) is exactly what I am looking for in order to import a number of tables into Excel via webqueries.

Do you think it would be possible to help me with a minor modification?

The webQuerySheet seems to working perfectly for me as I have had success changing the base URL as well as the column numbers and header values, etc., in order to suit my specific purposes.

Regarding the horseData sheet, however, I have had limited success with the number of rows that are being archived:

The tables that I am importing on the webQuerySheet have 31 rows (30 Data + 1 Header) but it seems as if the code has been optimized for only 1 row of data. Or at least only the first row of data in the tables is being archived in the DataSheet at the moment (row 2, or 1st data row)...

Would you kindly let me know what needs to be done in order to change only importing 1 row (row 2) to importing 30 rows (rows 2 - 31)?

I would REALLY appreciate it.

I have pasted my modified version of the code, including URL's etc. here:



Option Explicit

Public Sub Extract_Horse_Data()

Dim baseURL As String, horseURL As String
Dim webQuerySheet As Worksheet, horseDataSheet As Worksheet
Dim i As Integer
Dim horseQuery As QueryTable
Dim horseID As Long
Dim horseDataRow As Long

Debug.Print Now; "Started"

'Base URL with HorseID parameter omitted. The parameter name and value is appended to the web query when requesting
'each horse

baseURL = "http://www.arbets.com/past_results/"

'Sheet where web data for each horse is copied to

Set horseDataSheet = Worksheets("Sheet1")

'Sheet where web query is created

Set webQuerySheet = Worksheets("Sheet3")

'Set starting row on horse data sheet for copying horse web data to. If A1 is empty the sheet is cleared and initialised
'with column headers

With horseDataSheet

If .Range("A1").Value = "" Then

'Initialise horse data sheet with column headers in row 1, and set starting row to row 2

.UsedRange.ClearContents
.Range("A1").Resize(1, 7).Value = Array("URL", "Num.", "Sport", "Match", _
"Bookmakers", "Profit", "Time (GMT)", "Duration")

horseDataRow = 2

Else

'Set starting row to first empty row found in column A

horseDataRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1

End If

End With

'Delete all web queries on webQuerySheet

webQuerySheet.UsedRange.ClearContents
For i = webQuerySheet.QueryTables.Count To 1 Step -1
Debug.Print Now; "Deleting query table " & webQuerySheet.QueryTables(i).Name
webQuerySheet.QueryTables(i).Delete
Next

'Create the web query for retrieving horse data

Set horseQuery = Create_WebQuery(webQuerySheet)

If Not horseQuery Is Nothing Then

'Request web data for each HorseID, copying details to next row on horse data sheet

For horseID = 1 To 10
horseURL = baseURL & "?p=" & horseID
Get_Horse_Data horseQuery, horseURL, horseDataSheet.Cells(horseDataRow, 1)
horseDataRow = horseDataRow + 1
Next

End If

Debug.Print Now; "Finished"

End Sub


Private Sub Get_Horse_Data(QT As QueryTable, sURL As String, copyToRange As Range)

'Request web data for a specific horse. If the horse exists its details are copied to the horse data sheet, otherwise
'blank details are copied

Dim savedErr As ErrObject

QT.Connection = "URL;" & sURL

On Error Resume Next 'Trap possible errors from refreshing web query

QT.Refresh BackgroundQuery:=False

If Err.Number = 0 Then

'No error occurred - copy data from retrieved web data to data sheet

On Error GoTo 0
Debug.Print Now; sURL & " - Retrieved OK"

With copyToRange
.Offset(0, 0) = sURL

'An owner name of "TRANSFER HISTORY" means the horse doesn't exist and all the details copied are blank

If QT.Destination.Range("A5").Value <> "TRANSFER HISTORY" Then
.Offset(0, 1) = QT.Destination.Range("A5").Value 'Owner name
End If
.Offset(0, 1) = QT.Destination.Range("A2").Value 'Num.
.Offset(0, 2) = QT.Destination.Range("B2").Value 'Sport
.Offset(0, 3) = QT.Destination.Range("C2").Value 'Match
.Offset(0, 4) = QT.Destination.Range("D2").Value 'Bookmakers
.Offset(0, 5) = QT.Destination.Range("E2").Value 'Profit
.Offset(0, 6) = QT.Destination.Range("F2").Value 'Time (GMT)
.Offset(0, 7) = QT.Destination.Range("G2").Value 'Duration
End With

Else

'An unexpected error occurred - tell the user

Set savedErr = Err
On Error GoTo 0
Debug.Print Now; sURL & " - Error " & savedErr.Number & " " & savedErr.Description

MsgBox "Weq query URL: " & sURL & vbNewLine & _
"Error number " & savedErr.Number & vbNewLine & _
savedErr.Description, , "Web query error"

End If

End Sub


Private Function Create_WebQuery(webQuerySheet As Worksheet) As QueryTable

'Create web query. Note that a URL is not specified in the Connection string, but instead is specified when querying
'each horse

Set Create_WebQuery = webQuerySheet.QueryTables.Add(Connection:="URL;", Destination:=webQuerySheet.Range("A1:a10"))

If Not Create_WebQuery Is Nothing Then
With Create_WebQuery
.Name = "TransferHistory.asp"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False 'Wait for query to return before requesting next one (was True with Macro Recorder)
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 1
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
'.Refresh BackgroundQuery:=False 'Refresh when retrieving each URL, not here
End With
Else
MsgBox "Error creating web query" & vbNewLine & vbNewLine & _
"Error number = " & Err.Number & vbNewLine & Err.Description
End If

End Function
 
Upvote 0

Forum statistics

Threads
1,216,189
Messages
6,129,406
Members
449,509
Latest member
ajbooisen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top