Error message from web query , Please help !

TenTen

New Member
Joined
May 1, 2020
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hi guys,

I need your help.

Since I'm running web query in every 1 minute to update data from the website.

1588392268270.png


The thing is if I loose the internet connection, the error message window will be shown and that stopping my excel sheet from continue gathering the data from website until I click "OK" button.

1588391934276.png


Is there any ways to fix this ? either automatically click "OK" on the error message window or disable the error message window ?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Trying to reinsert didn't worked?

When I've the same error I usually reinsert it.
 
Upvote 0
I don't think you can trap this error in the Power Query formula (M language).

One way is to set up a VBA BeforeRefresh query handler which checks the internet connection status.
 
Upvote 0
I don't think you can trap this error in the Power Query formula (M language).

One way is to set up a VBA BeforeRefresh query handler which checks the internet connection status.

Thank you for replying !

That's a good idea however I've no coding skill.

Could you sample the VBA BeforeRefresh query handler ,so I can adjust to suit my excel sheet.
 
Upvote 0
My code as below, Could anyone help ?

VBA Code:
Sub WebQuery()

With Sheets("Test").QueryTables.Add(Connection:= _
"URL;https://www.investing.com/crypto/xrp/xrp-usd", _
Destination:=Range("A1"))

.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = True
.RefreshPeriod = 1
.RefreshStyle = xlOverwriteCells
.FieldNames = True
.WebSelectionType = xlSpecifiedTables
.WebTables = 4
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False

        
        
End With
End Sub
 
Upvote 0
These 3 pieces of code implement the BeforeRefresh query handler and must be put in the correct modules. It works with a query added manually with the Excel UI or a query added with VBA code, as your last post does. It expects the query to be on a sheet named "Sheet1".

Put this code in a class module (e.g. Class1), and IMPORTANT, change its Name property to clsQuery instead of Class1:
VBA Code:
Option Explicit

Public WithEvents Query As QueryTable

Private Sub Query_BeforeRefresh(Cancel As Boolean)
    Cancel = Not IsInternetConnected()
End Sub
Put this code in a standard module (e.g Module1):
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function InternetGetConnectedStateEx Lib "Wininet.dll" (ByRef lpdwFlags As LongPtr, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Boolean
#Else
    Private Declare Function InternetGetConnectedStateEx Lib "Wininet.dll" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Boolean
#End If

Dim PQtable As clsQuery


Public Sub Create_Query_Handler()
 
    Dim qt As QueryTable
 
    Set qt = ThisWorkbook.Worksheets("Sheet1").ListObjects(1).QueryTable  
    Set PQtable = New clsQuery
    Set PQtable.Query = qt

End Sub


Public Function IsInternetConnected() As Boolean

#If VBA7 Then
    Dim flags As LongPtr
#Else
    Dim flags As Long
#End If
    Dim connectionName As String * 255
  
    IsInternetConnected = InternetGetConnectedStateEx(flags, connectionName, LenB(connectionName), 0)

End Function
With your query set to an automatic periodic refresh (e.g. 1 minute) you can set up and test the BeforeRefresh handler by running the above Create_Query_Handler macro just once.

In order to set up the handler every time you open the workbook, put this code in the ThisWorkbook module:

VBA Code:
Option Explicit

Private Sub Workbook_Open()
    Create_Query_Handler
End Sub
Then save, close and reopen the workbook to test it.
 
Upvote 0
These 3 pieces of code implement the BeforeRefresh query handler and must be put in the correct modules. It works with a query added manually with the Excel UI or a query added with VBA code, as your last post does. It expects the query to be on a sheet named "Sheet1".

Put this code in a class module (e.g. Class1), and IMPORTANT, change its Name property to clsQuery instead of Class1:
VBA Code:
Option Explicit

Public WithEvents Query As QueryTable

Private Sub Query_BeforeRefresh(Cancel As Boolean)
    Cancel = Not IsInternetConnected()
End Sub
Put this code in a standard module (e.g Module1):
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function InternetGetConnectedStateEx Lib "Wininet.dll" (ByRef lpdwFlags As LongPtr, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Boolean
#Else
    Private Declare Function InternetGetConnectedStateEx Lib "Wininet.dll" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Boolean
#End If

Dim PQtable As clsQuery


Public Sub Create_Query_Handler()

    Dim qt As QueryTable

    Set qt = ThisWorkbook.Worksheets("Sheet1").ListObjects(1).QueryTable
    Set PQtable = New clsQuery
    Set PQtable.Query = qt

End Sub


Public Function IsInternetConnected() As Boolean

#If VBA7 Then
    Dim flags As LongPtr
#Else
    Dim flags As Long
#End If
    Dim connectionName As String * 255

    IsInternetConnected = InternetGetConnectedStateEx(flags, connectionName, LenB(connectionName), 0)

End Function
With your query set to an automatic periodic refresh (e.g. 1 minute) you can set up and test the BeforeRefresh handler by running the above Create_Query_Handler macro just once.

In order to set up the handler every time you open the workbook, put this code in the ThisWorkbook module:

VBA Code:
Option Explicit

Private Sub Workbook_Open()
    Create_Query_Handler
End Sub
Then save, close and reopen the workbook to test it.

John_w

Appreciate for your answer.

With little knowledge, I still can't do it ?.

1588526497694.png


1588526544623.png


1588526657683.png
 
Upvote 0
You have put the code in correct modules.

What is the error message?

How many tables are on "Sheet1"? The code expects the query to be in the first table [ListObjects(1)] on the sheet. If there is more than one table then try changing the (1) to (2), etc.

If that doesn't fix the error try replacing the yellow line with:
VBA Code:
    Set qt = ThisWorkbook.Worksheets("Sheet1").ListObjects("XRP_USD_Quotes").QueryTable
If it still doesn't work, I suggest you create the query in a new workbook and add the code.
 
Upvote 0
I've adjusted the code as you mentioned and it can run smoothly.

However I tried to disconnect the internet and wait for query to automatically run in 1 min time period, still showing the message window and that freezes the excel sheet.

Is it possible to have the excel sheet to keep running without being stopped ? even if there is no internet connection ?

1588552864596.png


I've found some threads and I think it's the same as my case.


It seems to be resolved which I don't understand how.

Could you please clarify on this ?

Thank you for your patience ! :giggle:
 
Upvote 0
This is very strange, because I expected Cancel = Not IsInternetConnected() to set Cancel to False if you have an Internet connection and True if not, thereby cancelling the refresh for the latter case. However Cancel is always True. To fix this replace Query_BeforeRefresh with:

VBA Code:
Private Sub Query_BeforeRefresh(Cancel As Boolean)
    If IsInternetConnected Then
        Cancel = False
    Else
        Cancel = True
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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