Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
web抓取后使用VBA删除Excel中的连接_Vba_Excel_Connection - Fatal编程技术网

web抓取后使用VBA删除Excel中的连接

web抓取后使用VBA删除Excel中的连接,vba,excel,connection,Vba,Excel,Connection,我有一个基于动态链接的网络连接。因此,我无法设置固定连接。以下宏创建连接,然后更新工作表 With ThisWorkbook.Worksheets("Data").QueryTables.Add(Connection:= _ "<URL redacted>", Destination:=ThisWorkbook.Worksheets("Data").Range("$A$1")) .Name = "DataPull" .FieldName

我有一个基于动态链接的网络连接。因此,我无法设置固定连接。以下宏创建连接,然后更新工作表

With ThisWorkbook.Worksheets("Data").QueryTables.Add(Connection:= _
        "<URL redacted>", Destination:=ThisWorkbook.Worksheets("Data").Range("$A$1"))
        .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
但我不想删除另外两个固定连接。
创建的新连接具有名称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
Dim Conn作为工作簿连接
用于此工作簿中的每个连接。连接
如果连接名为“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