[英]EXCEL VBA - Webquery: webpage redirection
因此,我遇到了以下問題:我試圖進行一個Web查詢,該WebQuery將從一個網站上獲取數據,但是問題是,當您第一次嘗試轉到該頁面時,該網頁將重定向到另一個網站。 並且該鏈接僅在第二次嘗試時打開(無重定向)。
我需要在代碼中添加些什么,以便擺脫重定向問題? 添加循環?
這是代碼:
Sub Auto_open()
Dim newcon as String
Cells.Select
Selection.QueryTable.Delete
Selection.ClearContents
Range("A1").Select
newcon = "an URL"
With ActiveSheet.QueryTables.Add(Connection:="URL;" + newcon _
, Destination:=Range("$A$1"))
.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 = "2"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
嘗試禁用重定向
.WebDisableRedirections = True
如果這樣不起作用,請嘗試在幾秒鍾后重新刷新查詢
Application.Wait(Now + TimeValue("0:00:10")) .Refresh
如果那不起作用,我想考慮使用Webbrowser控件。
您可以提供url鏈接進行測試嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.