Remove old connections with VBA

ServerDude

New Member
Joined
Mar 31, 2011
Messages
30
HI,

I have seen multiple posts about removing old connections using VBA but they remove all connections. I have a sheet with 62000+ connections that have built up over time and would like to remove them and retain the 100 usable connections. Is there a way i can filter against the "Last Refreshed" criteria? The code i have used so far is:

Sub Remove()
Dim connection As WorkbookConnection
On Error Resume Next
For Each connection In ThisWorkbook.Connections
connection.Delete
Next
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Please use code tags when you post code. See example in red/blue below


I haven't tested the below code, but it should work. Please read the comments
Rich (BB code):
Option Explicit

Sub Remove()
    Dim conConnect As WorkbookConnection
    'you had your variable named connection. That is quite dangerous as _
      there is a property called connections. A small typo and things go haywire. _
      I also always use at least on Capital letter in a variable, so I can see if _
      I make a typo: The vba editor should automatically modify the variable with _
      the capital letter.
    
    'On Error Resume Next '<<< why are you using this? _
      If you don't need it it can only lead to very difficult to spot bugs. _
      You will also have to reset its behaviour after the line where it was _
      needed  by using  "On Error Goto 0"
    
    For Each conConnect In ThisWorkbook.Connections
        With conConnect
            'Delete connection if older than 30 days
            If .DataFeedConnection.RefreshDate < (Date - 30) Then
                conConnect.Delete
            End If
        Next conConnect
End Sub
 
Upvote 0
Hi and thanks fro your reply.

I have copied in and am getting a compile error or "Next without For"

Thanks in advance.

Jason
 
Upvote 0
End With was missing
VBA Code:
    For Each conConnect In ThisWorkbook.Connections
        With conConnect
            'Delete connection if older than 30 days
            If .DataFeedConnection.RefreshDate < (Date - 30) Then
                conConnect.Delete
            End If
        End With          
    Next conConnect
 
Upvote 0
Hi Guys,

I am now getting an error or the object is not supported when using the date - 30. I have also tried to replace the date with an actual date but it still fails.

I am running Excel 2010. Is the above code compatible?

Date-issue1.PNG
Date-issue2.PNG


I have noticed that in your script above the Date - 30 is colour coded, 30 in red, on my script it is in black.

Thanks,

Jason
 
Upvote 0
Try this

VBA Code:
    Dim conConnect As WorkbookConnection, D As Date
    D = Date - 30
    For Each conConnect In ThisWorkbook.Connections
        With conConnect
            If .DataFeedConnection.RefreshDate < D Then
                conConnect.Delete
            End If
        End With
    Next conConnect
 
Upvote 0
Hi Yongle,

Thanks again, but still have the same error run time error "438". "Object doesn't support this object or method"
Date-issue3.PNG
 
Upvote 0
For date calculations apparently we need to use a function DateDiff

DateDiff("d",Date1,Date2) will give the number of days between the two dates. So we need to use:
VBA Code:
        With conConnect
            ' Check if the date is more then 30 days ago
            If DateDiff("d",.DataFeedConnection.RefreshDate , Date) > 30 Then
                conConnect.Delete
            End If
        End With
 
Upvote 0
I am really sorry to keep pestering,

still the same error. This is what i have.

Date-issue4.PNG


Again, i am not seeing the correct colour format of the 30.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,171
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top