Killing connection in EXCEL vba

前端 未结 2 1216
庸人自扰
庸人自扰 2021-01-18 03:46

I am trying to removeconnection from my work book but I am still geting run-time error 5. I dont know what to do because in my other projects it works.

Thanks for ad

相关标签:
2条回答
  • 2021-01-18 04:08

    You could try this in the for loop for deleting, using the minimal index 1 (One = 2/2) in VBA in place of i variable:

    ActiveWorkbook.Connections.Item(1).Delete
    

    Instead of

    ActiveWorkbook.Connections.Item(i).Delete
    

    As you delete, ActiveWorkbook.Connections.Count() will diminish, Some .item(i) does no more exist.

    Or this:

     '~~>kill all connections
        For i = ActiveWorkbook.Connections.Count To 1 Step -1
            ActiveWorkbook.Connections.Item(i).Delete
        Next
    
    0 讨论(0)
  • 2021-01-18 04:10

    Why not using the built-in enumerator of the connections collection?

    Public Sub DeleteAllConnectionsInWorkbook()
        Dim aConn as Object
        For Each aConn in ActiveWorkbook.Connections
            aConn.Delete
        Next aConn
    End Sub
    
    0 讨论(0)
提交回复
热议问题