简体   繁体   中英

Excel VBA QueryTables Preserve Hyperlinks

I use QueryTables quite a lot in VBA to scrape data from webpages. I wondered if there was a way to preserve the hyperlinks that the webpage the query is pulling data from has.

Here is an example Query:

With ActiveSheet.QueryTables.Add(Connection:= _
 "URL;http://dom.com/table, Destination:=Range("$A$1"))
 .Name = "table"
 .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 = xlWebFormattingRTF
 .WebTables = """datatable"""
 .WebPreFormattedTextToColumns = True
 .WebConsecutiveDelimitersAsOne = True
 .WebSingleBlockTextImport = False
 .WebDisableDateRecognition = False
 .WebDisableRedirections = False
 .Refresh BackgroundQuery:=False
End With

The trick was changing .WebFormatting = xlWebFormattingRTF to .WebFormatting = xlWebFormattingAll

Additional Resources:

https://docs.microsoft.com/en-us/office/vba/api/excel.querytable.webformatting https://docs.microsoft.com/en-us/office/vba/api/excel.xlwebformatting

Name              Value Description
xlWebFormattingAll  1   All formatting is imported.
xlWebFormattingNone 3   No formatting is imported.
xlWebFormattingRTF  2   Rich Text Format - compatible formatting is imported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM