+ Reply to Thread
Results 1 to 6 of 6

Refresh data and close connections after opening workbook

  1. #1
    Registered User
    Join Date
    12-05-2012
    Location
    london
    MS-Off Ver
    2010
    Posts
    4

    Refresh data and close connections after opening workbook

    Hi all,

    I have no experience in VBA. I have a workbook that at the moment pulls external data from access and automatically updates a pivot table.
    I want it so when I open the file it refreshes the data and then deletes the connection.

    I have found two codes that work when I use them seperately, but when i triy to combine I have no luck. They are below...


    Sub WorkBook_Open()
    ActiveWorkbook.RefreshAll
    End Sub

    Sub WorkBook_Open()
    For i = 1 To ActiveWorkbook.Connections.Count
    If ActiveWorkbook.Connections.Count = 0 Then Exit Sub
    ActiveWorkbook.Connections.Item(i).Delete
    i = i - 1
    Next i
    End Sub


    Thanks for your help

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Refresh data and close connections after opening workbook

    Try this

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    12-05-2012
    Location
    london
    MS-Off Ver
    2010
    Posts
    4

    Re: Refresh data and close connections after opening workbook

    Thanks for your reply. The refresh works but the connection still doesn't delete!

  4. #4
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Refresh data and close connections after opening workbook

    OH haha. You don't need to use items

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-05-2012
    Location
    london
    MS-Off Ver
    2010
    Posts
    4

    Re: Refresh data and close connections after opening workbook

    The connection is still not deleting. If I take out the ActiveWorkbook.RefreshAll, then the connection delete does work. Is there something minor missing?

  6. #6
    Registered User
    Join Date
    12-05-2012
    Location
    london
    MS-Off Ver
    2010
    Posts
    4

    Re: Refresh data and close connections after opening workbook

    Is anyone able to help? Just to recap, when I open the workbook I want it to automatically refresh and then delete the connection.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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