iknowu99
Well-known Member
- Joined
- Dec 26, 2004
- Messages
- 1,158
- Office Version
- 2016
Aloha everybody,
I am runing the following code yet it doesnt seem the best way to do it. The goal is to get data from online everytime the time stamp changes.
I am runing the following code yet it doesnt seem the best way to do it. The goal is to get data from online everytime the time stamp changes.
Code:
'excel2007
Sub macro()
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
strURL = "http://www.yahoo.com" 'actually another website that has a timestamp and goes to A1
xmlhttp.Open "GET", strURL, True, "", "" '
Call xmlhttp.Send
' Lets set a timeout of 3 seconds
timeout = Timer + 3
While xmlhttp.readyState <> 4 And timeout > Timer
' Lets prevent the script from feezing
DoEvents
Wend
' Is the call okay - 200 is the HTTP response code for OK but it does not work for me so commented out
If xmlhttp.readyState = 4 Then 'And xmlhttp.Status = 200 Then
RtnPage = xmlhttp.responseText
Else
nothingNew = True
GoTo endSUB
End If
Set fs = CreateObject("Scripting.FileSystemObject")
Set ah = fs.CreateTextFile("C:\default.html", True)
ah.Writeline (RtnPage)
ah.Close
'there is a query to refer to default.html that just got saved to C:\
Sheets("Sheet1").QueryTables(1).Refresh BackgroundQuery:=False
'it does not reference online directly because there were some problems with updating so often from online query so i used the html way. still cant find a clear direction as to how to utilize the refresh with under every minute run...and it seems to me xmlHTTP way is faster so i prefer it
GeneratedA1 = Sheets("Sheet1").Range("A1")
If GeneratedA1 = "" Then
nothingNew = True
GoTo endSUB
End If
'everytime A1 in Sheet1 should be updated as the time the data is received, this happens on the original website, so if new time stamp is created online then the sub creates "nothingNew = false"
With Sheets("Tracking")
LastRowTrack = .Cells(65536, "A").End(xlUp).Row + 1
If .Cells(LastRowTrack - 1, 1) = GeneratedA1 Then
nothingNew = True
GoTo endSUB
End If
.Cells(LastRowTrack, 1) = GeneratedA1
.Cells(LastRowTrack, 2) = Now
End With
endSUB:
End Sub