Automatically update dependent connections when closing file

DHero

New Member
Joined
Sep 22, 2021
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hello, I have a database(xlsb) to which a query connects and dumps the resulting table on another file(xlsm) and I'm trying to develop a piece fo code that, upon closing the database(xlsb) file it automatically updates the dependant file(xlsm). I've tried using that same file (xlsb) but it kep throwing errors, I then precceeded to add to the code a line that "saved as" the xlsb file and then connected the dependant file(xlsm) to this new file that is always created. This still doesnt work as when I open the dependant file(xlsm) the data is not updated. How can I make this code work? Following is the current code.

Rich (BB code):
Sub Workbook_BeforeClose(cancel As Boolean)
    
    CarryOn = MsgBox("Atualizar planilhas dependentes? (Pode levar um minuto)", vbYesNo, "Atualizar planilhas dependentes")
    If CarryOn = vbYes Then
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        'Application.EnableEvents = False
        ThisWorkbook.SaveAs Filename:= _
        "R:path\file.xlsb" _
        , FileFormat:=50
        Dim strFilename As String: strFilename = "R:path\file.xlsm"
        Dim GPSMANUT As Workbook
        Set GPSMANUT = Workbooks.Open(Filename:=strFilename)
        GPSMANUT.RefreshAll
        GPSMANUT.Connections("Consulta - Base de dados").Refresh
        GPSMANUT.Connections("Consulta - Apoio$_FilterDatabase").Refresh
        GPSMANUT.Save
        GPSMANUT.Close
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        'Application.EnableEvents = True
        ThisWorkbook.Save
        Beep
        MsgBox "Planilhas dependentes atualizadas!"
    End If
End Sub


Private Sub Workbook_Open()
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I think it is likely due to the use of "Workbook_BeforeClose"
Try using "Workbook_Close"
 
Upvote 0
That ain't it chief, the code runs, opens the .xlsm even, but the lines that are supposed to update the queries aren't doing their job
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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