+ Reply to Thread
Results 1 to 7 of 7

Deleting External Connections

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    Excel 365
    Posts
    129

    Deleting External Connections

    Hello,

    I've got one workbook that I use as a template to generate reports. It parses different files and puts into into a readable format. This workbook is taking longer and longer for it to open and save so I am trying to speed it up. I think part of this is due to all the external connections that have been stored in the workbook. Unfortunately, if I try to delete these connections, my macro is no longer able to load up the file. Is there a better way of doing this?

    I should note that the files are constantly changing. So a file named 0418.csv may be different tomorrow than what it is today and a new report is generated. So each time I run my report macro, I clear out the imported data and reload the file. It appears that this has caused multiple connections like 04181, 04182, 04183,etc.

    Please help!

    Also, I just noticed this so there are a couple hundred external connections. If I try to select all and remove, it locks up and crashes Excel. Does anyone have a macro that would clean this up better?

    Thanks!

  2. #2
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    Excel 365
    Posts
    129

    Re: Deleting External Connections

    This is the error I get when I've deleted the external connection and try running my macro again...

    Automation Error:

    The object invoked has disconnected from its clients:


        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" + csvPath & FileName, Destination:=Range("$A$1"))
    Last edited by rjj920; 04-10-2018 at 08:21 PM.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Deleting External Connections

    Hello rjj920,

    This macro will remove them. Add a New VBA module to your workbook and paste this code into it. You can run the macro using the ALT+F8 while in Excel to bring up the macro dialog.

    Sub RemoveConnections()
    
        Dim qt As Object
        
            For Each qt In ActiveSheet.QueryTables
                qt.Delete
            Next qt
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    Excel 365
    Posts
    129

    Re: Deleting External Connections

    Thanks, Leith. I am still having a hard time reestablishing the connection to pull that same file and generating a new report (i.e. when I remove the connection 0418.csv and then rerun my macro to repull the new 0418.csv file, I get the cannot connect error). I'm hoping there is an easy way to generate new connections automatically without too much hassle.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Deleting External Connections

    Hello rjj920,

    It would help to see the code you are using for making the connection.

  6. #6
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    Excel 365
    Posts
    129

    Re: Deleting External Connections

    Sheets("Data").Select
        Cells.Select
        Range("S1").Activate
        Selection.QueryTable.Delete
        Selection.ClearContents
        Range("A1").Select
        
        
        Worksheets("Data").Cells.Clear
        Range("A1").Select
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" + csvPath & FileName, Destination:=Range("$A$1"))
            .Name = FileName
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With

  7. #7
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    Excel 365
    Posts
    129

    Re: Deleting External Connections

    Would there be a better way to reconnect a connection (or rather start a new one) than this line?

        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" + csvPath & FileName, Destination:=Range("$A$1"))

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. External data connections are removed
    By edk74 in forum Excel General
    Replies: 1
    Last Post: 01-18-2018, 07:37 AM
  2. Replies: 2
    Last Post: 02-18-2017, 04:11 AM
  3. [SOLVED] Work in connections with external data
    By kiluwa99 in forum Excel General
    Replies: 3
    Last Post: 06-28-2016, 01:03 AM
  4. Replies: 0
    Last Post: 05-14-2015, 05:16 AM
  5. external data connections are not refreshing even with VBA
    By krishna2221 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-07-2014, 04:00 AM
  6. When are External Connections not QueryTables?
    By apolloni in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-07-2013, 08:11 AM
  7. external data - cleanup existing connections
    By edstevens in forum Excel General
    Replies: 1
    Last Post: 04-26-2013, 12:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1