返回列表 上一主題 發帖

(發問)WEB查詢,如何取消跳出的警告視窗

本帖最後由 GBKEE 於 2011-11-1 12:59 編輯

回復 3# jewayy
試試看
  1. Sub Ex()
  2. Dim t As Date
  3.     t = Time
  4.     On Error Resume Next
  5. 1:
  6.     Sheet1.QueryTables(1).Refresh False
  7.     Debug.Print Err.Number                 '即時運算視窗查看 Err.Number
  8.     If Err.Number > 0 Then                 'Web查詢 失敗
  9.         If Time > t + #12:00:10 AM# Then   '連線時間超過1分鐘
  10.             If MsgBox("連線時間超過1分鐘 繼續Web查詢 ??", vbYesNo) = vbNo Then Exit Sub
  11.             t = Time
  12.         End If
  13.         GoTo 1
  14.     End If
  15. End Sub
複製代碼
  1. Sub Ex1()
  2.     Dim t As Date
  3.     t = Time
  4.     On Error Resume Next
  5.     Do
  6.         Err.Clear
  7.         Sheet1.QueryTables(1).Refresh False
  8.         If Err.Number > 0 Then                 'Web查詢 失敗
  9.             If Time > t + #12:00:10 AM# Then   '連線時間超過1分鐘
  10.                 If MsgBox("連線時間超過1分鐘 繼續Web查詢 ??", vbYesNo) = vbNo Then Exit Sub
  11.                 t = Time
  12.             End If
  13.         End If
  14.     Loop While Err.Number > 0
  15. End Sub
複製代碼

TOP

回復 5# jewayy
  1. Sub Ex1()
  2.     Dim t As Date
  3.     t = Time
  4.     On Error Resume Next
  5.     With Sheet1
  6.         .Range("A1:A3").Value = .Range("C1:C3").Value
  7.     End With
  8.     With Sheet2
  9.         .Cells.Interior.ColorIndex = xlNone
  10.         For I = 1 To .QueryTables.Count
  11.             Err.Clear
  12.             .QueryTables(I).Refresh False
  13.             If Err.Number > 0 Then                 'Web查詢 失敗
  14.                 With .QueryTables(I).ResultRange
  15.                     .Interior.ColorIndex = 37
  16.                     .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count) = "查無資料"
  17.                 End With
  18.             End If
  19.         Next I
  20.     End With
  21. End Sub
複製代碼

TOP

本帖最後由 GBKEE 於 2011-11-17 08:05 編輯

回復 7# jewayy
奇怪 測試沒問題的, 怎又出現 ,請改用物件類別模組.
請在 VAB中插入一 物件類別模組  會自動名為 Class1
執行 Ex
Module1的程式碼
  1. Sub Ex()
  2.     Dim i As Integer, Test() As New Class1
  3.     'Test指定為 新物件類別模組 :  Class1物件
  4.     On Error Resume Next
  5.     For i = 1 To Sheet2.QueryTables.Count
  6.         ReDim Preserve Test(1 To i)
  7.         Set Test(i).Query = Sheet2.QueryTables(i)
  8.         Test(i).Query.Refresh False
  9.     Next
  10. End Sub
複製代碼
物件類別模組   : Class1 的程式碼
  1. Option Explicit
  2. Public WithEvents Query As QueryTable    'Query指定為QueryTable物件
  3. Private Sub Query_AfterRefresh(ByVal Success As Boolean)   '查詢後的事件
  4.   If Success = False Then    '查詢失敗   'Success = True   查詢成功
  5.         With Query.ResultRange
  6.             .Interior.ColorIndex = 37
  7.             .Cells(1).Offset(1).Resize(.Rows.Count - 1, .Columns.Count) = "查無資料"
  8.         End With
  9.   End If
  10. End Sub
  11. Private Sub Query_BeforeRefresh(Cancel As Boolean)  '查詢前的事件
  12.     With Query.ResultRange
  13.             .Interior.ColorIndex = xlNone
  14.     End With
  15. End Sub
複製代碼

TOP

回復 9# jewayy
沒辦法解決    你的 Web查詢 設定參數 變動   系統  自動更新資料    VBA無法控制   
   
  1. With Sheet1
  2.             .Range("A1:A3").Value = .Range("C1:C3").Value    '啟動 系統  自動更新資料
  3.         End With
複製代碼

TOP

回復 13# blue2263
以下是我的見解
  1.    On Error GoTo 101 '   'web 查無 到下一個代碼  
  2. ** 單一次的Refresh失敗 On Error GoTo 101 可以處理
  3. ** 但連續的Refresh失敗 On Error GoTo 101 無法處理(無解)
  4.              .Range("az7").QueryTable.Refresh BackgroundQuery:=False
  5. QueryTable.Refresh BackgroundQuery:=False           
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 看別人不順眼,是自己修養不夠。
返回列表 上一主題