网络抓取后使用 VBA 删除 Excel 中的连接
Delete connection in Excel using VBA after web scraping
我有一个基于动态 link 的网络抓取连接。所以,我无法设置固定连接。以下宏创建连接,然后更新工作表。
With ThisWorkbook.Worksheets("Data").QueryTables.Add(Connection:= _
"<URL redacted>", Destination:=ThisWorkbook.Worksheets("Data").Range("$A"))
.Name = "DataPull"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
.Delete
End With
此宏每 1 分钟运行一次以更新数据。因此,这会在每次运行时创建一个新连接。我不希望存在这么多连接,因为它们将不再被使用。
网络抓取完成后如何删除连接?
或者有没有办法建立一个可以根据变量修改自身的连接。变量是根据当前时间变化的时间间隔。
我看了这个选项
For Each qr In ThisWorkbook.Queries
qr.Delete
Next qr
但是我不想删除另外两个固定连接。
创建的新连接的名称为 Connection、Connection1 等。有没有办法根据名称删除连接?
假设您要删除除 Connection1 和 Connection2 之外的所有连接,请尝试...
Dim Conn As WorkbookConnection
For Each Conn In ThisWorkbook.Connections
If Conn.Name <> "Connection1" And Conn.Name <> "Connection2" Then
Conn.Delete
End If
Next Conn
希望对您有所帮助!
我有一个基于动态 link 的网络抓取连接。所以,我无法设置固定连接。以下宏创建连接,然后更新工作表。
With ThisWorkbook.Worksheets("Data").QueryTables.Add(Connection:= _
"<URL redacted>", Destination:=ThisWorkbook.Worksheets("Data").Range("$A"))
.Name = "DataPull"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
.Delete
End With
此宏每 1 分钟运行一次以更新数据。因此,这会在每次运行时创建一个新连接。我不希望存在这么多连接,因为它们将不再被使用。 网络抓取完成后如何删除连接? 或者有没有办法建立一个可以根据变量修改自身的连接。变量是根据当前时间变化的时间间隔。
我看了这个选项
For Each qr In ThisWorkbook.Queries
qr.Delete
Next qr
但是我不想删除另外两个固定连接。 创建的新连接的名称为 Connection、Connection1 等。有没有办法根据名称删除连接?
假设您要删除除 Connection1 和 Connection2 之外的所有连接,请尝试...
Dim Conn As WorkbookConnection
For Each Conn In ThisWorkbook.Connections
If Conn.Name <> "Connection1" And Conn.Name <> "Connection2" Then
Conn.Delete
End If
Next Conn
希望对您有所帮助!