Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-12-12 09:44:04
Point:500 Replies:9 POST_ID:828440USER_ID:11059
Topic:
Visual Basic Programming;Microsoft Access Database;Microsoft Excel Spreadsheet Software
I try to use VBA in Excel Querytable function to extract stock table from yahoo for Web Query Table tutorial
The VBA code I write 's result is not stable at all, sometimes, it can be extracted but sometimes not and
sometimes, have run-time error 1004, sometimes not, really fuzzy...
QUestion-1 Just want to know is it the table too big to be exracted in query from my URL,
sconn = "URL; http://finance.yahoo.com/q/hp?s=%5EDJI&d=11&e=12&f=2010&g=d&a=9&b=1&c=1928&z=66&y=" & i * 66
where i is integer from 0 to 8 ? How can I set max buffer size for query table and loading timeout time
in order to solve my issue if buffer size and timeout time is root cause?
Question-2, If possible, could you help to try my code at your side to see whether my IE or system setting to cause
the problem. I am using IE7 and windows vista ,excel2003?
Question-3, how to use .name with querytable.add, Is it also related to my problem?
Please advise
Duncan
The VBA code I write 's result is not stable at all, sometimes, it can be extracted but sometimes not and
sometimes, have run-time error 1004, sometimes not, really fuzzy...
QUestion-1 Just want to know is it the table too big to be exracted in query from my URL,
sconn = "URL; http://finance.yahoo.com/q/hp?s=%5EDJI&d=11&e=12&f=2010&g=d&a=9&b=1&c=1928&z=66&y=" & i * 66
where i is integer from 0 to 8 ? How can I set max buffer size for query table and loading timeout time
in order to solve my issue if buffer size and timeout time is root cause?
Question-2, If possible, could you help to try my code at your side to see whether my IE or system setting to cause
the problem. I am using IE7 and windows vista ,excel2003?
Question-3, how to use .name with querytable.add, Is it also related to my problem?
Please advise
Duncan
Option ExplicitPrivate Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Sub Yahoostock()Dim i As IntegerDim qtb As QueryTableFor Each qtb In Sheets("data").QueryTablesqtb.DeleteNextRange("A1:z2000").SelectSelection.ClearRange("A1").SelectErr.Cleari = 0Do Until i = 8Dim sconn As Stringsconn = "URL; http://finance.yahoo.com/q/hp?s=%5EDJI&d=11&e=12&f=2010&g=d&a=9&b=1&c=1928&z=66&y=" & i * 66Debug.Print sconnWith ActiveSheet.QueryTables.Add(Connection:=sconn, Destination:=Range("a" & (1 + i * 67))) ' .Name = "Quote: " & symb .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlOverwriteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = False .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "15" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False ' On Error Resume Next ' .Refresh BackgroundQuery:=False .Refresh BackgroundQuery:=False ' Sleep (5000)End Withi = i + 1LoopEnd Sub 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:
Attachment:yahoodow.xls
Author: duncanb7 replied at 2024-12-15 06:39:42
Thanks, Qlemo, your reply trigger me to think more forwards,
The code is completely working, if the number of time or times to refresh is too long and still failed I can ue
ontime to check as follows. Hope this thread help others who got similar issue
Sub CheckQueryRefreshState(qtbl As QueryTable)
If qtbl.Refreshing Then
'If the query is still refreshing, call the
'CheckQueryRefreshState again in one second.
Application.OnTime Now() + TimeValue("00:00:01"), _
"CheckQueryRefreshState"
Else
'Display the number of rows returned to the query table
'after the query table is refreshed.
MsgBox qtbl.ResultRange.Rows.Count & " Rows Returned"
End If
End Sub
The code is completely working, if the number of time or times to refresh is too long and still failed I can ue
ontime to check as follows. Hope this thread help others who got similar issue
Sub CheckQueryRefreshState(qtbl As QueryTable)
If qtbl.Refreshing Then
'If the query is still refreshing, call the
'CheckQueryRefreshState again in one second.
Application.OnTime Now() + TimeValue("00:00:01"), _
"CheckQueryRefreshState"
Else
'Display the number of rows returned to the query table
'after the query table is refreshed.
MsgBox qtbl.ResultRange.Rows.Count & " Rows Returned"
End If
End Sub
Author: duncanb7 replied at 2024-12-15 06:33:47
The issue is came from .refresh from VBA or windows is too short, so sometimes it is work sometimes not .
If we are not able to change register for timeout of refresh or (readystate for navigate web). We can
force the query table to re-do once refresh is fail until is refresh. I think re-do a few time, at least once
is working
If we are not able to change register for timeout of refresh or (readystate for navigate web). We can
force the query table to re-do once refresh is fail until is refresh. I think re-do a few time, at least once
is working
Expert: Qlemo replied at 2024-12-15 06:32:48
1. You are missing the goto label Redo
2. It would be much better to check the Err instead of the cell contents to find out there has been an error
3. Either you miss a On Error Goto 0 after the refresh, or should move the On Error Resume Next out of the loop.
2. It would be much better to check the Err instead of the cell contents to find out there has been an error
3. Either you miss a On Error Goto 0 after the refresh, or should move the On Error Resume Next out of the loop.
Author: duncanb7 replied at 2024-12-15 06:29:51
Thanks, Qlemo, your reply trigger me to think more forwards,
The code is completely working, if the number of time or times to refresh is too long and still failed I can ue
ontime to check as follows. Hope this thread help others who got similar issue
Sub CheckQueryRefreshState(qtbl As QueryTable)
If qtbl.Refreshing Then
'If the query is still refreshing, call the
'CheckQueryRefreshState again in one second.
Application.OnTime Now() + TimeValue("00:00:01"), _
"CheckQueryRefreshState"
Else
'Display the number of rows returned to the query table
'after the query table is refreshed.
MsgBox qtbl.ResultRange.Rows.Count & " Rows Returned"
End If
End Sub
The code is completely working, if the number of time or times to refresh is too long and still failed I can ue
ontime to check as follows. Hope this thread help others who got similar issue
Sub CheckQueryRefreshState(qtbl As QueryTable)
If qtbl.Refreshing Then
'If the query is still refreshing, call the
'CheckQueryRefreshState again in one second.
Application.OnTime Now() + TimeValue("00:00:01"), _
"CheckQueryRefreshState"
Else
'Display the number of rows returned to the query table
'after the query table is refreshed.
MsgBox qtbl.ResultRange.Rows.Count & " Rows Returned"
End If
End Sub
Author: duncanb7 replied at 2024-12-15 06:28:56
It is solved finally and completely with the following the code by error resume next and check whether refresh is done if the cell is empty or not
for destination range. Now it is working fine ,and without the code improvement add , it is still unstable. for double confirmation
So I believe the following code is working
i = 0
Do Until i = 8
Dim sconn As String
sconn = "URL; http://finance.yahoo.com/q/hp?s=%5EDJI&d=11&e=12&f=2010&g=d&a=9&b=1&c=1928&z=66&y=" & i * 66
Debug.Print sconn
With ActiveSheet.QueryTables.Add(Connection:=sconn, Destination:=Range("a" & (1 + i * 67)))
' .Name = "Quote: " & symb
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "9"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
On Error Resume Next
' Sleep (10000)
.Refresh BackgroundQuery:=False
Range("A" & 16 + i * 67).Select
If Range("A" & 16 + i * 67) = "" Then 'if the cell is empty, it imeans, fail refresh
GoTo Redo
End If
End With
i = i + 1
Redo:
Err.Clear
Loop
for destination range. Now it is working fine ,and without the code improvement add , it is still unstable. for double confirmation
So I believe the following code is working
i = 0
Do Until i = 8
Dim sconn As String
sconn = "URL; http://finance.yahoo.com/q/hp?s=%5EDJI&d=11&e=12&f=2010&g=d&a=9&b=1&c=1928&z=66&y=" & i * 66
Debug.Print sconn
With ActiveSheet.QueryTables.Add(Connection:=sconn, Destination:=Range("a" & (1 + i * 67)))
' .Name = "Quote: " & symb
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "9"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
On Error Resume Next
' Sleep (10000)
.Refresh BackgroundQuery:=False
Range("A" & 16 + i * 67).Select
If Range("A" & 16 + i * 67) = "" Then 'if the cell is empty, it imeans, fail refresh
GoTo Redo
End If
End With
i = i + 1
Redo:
Err.Clear
Loop
Expert: Qlemo replied at 2024-12-15 04:32:11
So you want to show how to fight with such trouble - understand that.
In that case you should implement an error handler around the Refresh call, e.g. by not incrementing the counter for the result range. The next loop will then try to read the same page again.
In that case you should implement an error handler around the Refresh call, e.g. by not incrementing the counter for the result range. The next loop will then try to read the same page again.
Author: duncanb7 replied at 2024-12-15 03:58:50
That is for Web query tutorial, and that is good example to solve issue from Webquery from website instead of download csv file
Assisted Solution
Expert: Qlemo replied at 2024-12-14 09:10:49
250 points GOOD
QueryTable.Name does just name the region of cells in Excel. If you do not provide it, it is generated from the URL and an incrementing number appended to create uniqueness. So this is absolutely unrelated.
If the Web Query results are unreliable, that is always a result of timeouts. Some parts of the site might need to long sometimes to get retrieved, so the internal used web browser (which is IE) might stop receiving further contents, rendering the page incomplete. Since you are already using synchronous queries (BackgroundQueries = false), there is nothing you can do about that, sorry.
For tutorials you should try to find a more reliable web page. Keep in mind that some sites do not allow for automated querying, even for teaching purposes. The Yahoo page used seems to be ok for use, since they even allow for downloading as CSV.
If the Web Query results are unreliable, that is always a result of timeouts. Some parts of the site might need to long sometimes to get retrieved, so the internal used web browser (which is IE) might stop receiving further contents, rendering the page incomplete. Since you are already using synchronous queries (BackgroundQueries = false), there is nothing you can do about that, sorry.
For tutorials you should try to find a more reliable web page. Keep in mind that some sites do not allow for automated querying, even for teaching purposes. The Yahoo page used seems to be ok for use, since they even allow for downloading as CSV.
Accepted Solution
Expert: SiddharthRout replied at 2024-12-13 06:35:09
250 points GOOD
Hi I had made a similar program for one of my clients but it used to extract values from Keywords rather than numbers.
For example "VTI" and then so on...
Also see this Addin which I believe does what you want...
http://fransking.blogspot.com/2006/06/getting-stock-prices-into-excel.html
Sid
For example "VTI" and then so on...
Also see this Addin which I believe does what you want...
http://fransking.blogspot.com/2006/06/getting-stock-prices-into-excel.html
Sid