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
 

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,524
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
 

ServerDude

New Member
Joined
Mar 31, 2011
Messages
30
Hi and thanks fro your reply.

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

Thanks in advance.

Jason
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,282
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
 

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,524
Thanks yongle for correcting that.
 

ServerDude

New Member
Joined
Mar 31, 2011
Messages
30
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
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,282
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
 

ServerDude

New Member
Joined
Mar 31, 2011
Messages
30
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
 

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,524
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
 

ServerDude

New Member
Joined
Mar 31, 2011
Messages
30
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.
 

Forum statistics

Threads
1,078,437
Messages
5,340,277
Members
399,361
Latest member
Linford

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top