+ Reply to Thread
Results 1 to 6 of 6

Run Macro when saving the workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    09-25-2020
    Location
    vienna
    MS-Off Ver
    365
    Posts
    19

    Run Macro when saving the workbook

    Hi guys.


    I Have 2 files, A and B. I want a Macro that runs from B every time I save the file. The Macro will open A and afterwards will close it again. So far the Macro looks as follows:

    Sub OpenA()

    Workbooks.Open Filename:="E:\CV3\A.xlsm"
    Dim FileToClose As String
    FileToClose = "E:\CV3\A.xlsm"
    Workbooks(Dir(FileToClose)).Close
    End Sub

    I have added an event BeforeSave on ThisWorkbook but is not executing the macro

    What I donīt know how to add to this code is:
    How to execute the Macro once I save "B" considering is not working
    Once "A" opens, there are references that need to be updated. How can I include that in the Code?
    "A" needs to save before close....I donīt know how to do that either....

    sorry if those are very simple questions but Iīm not a pro on this XD.

    Thanks in advance!
    Last edited by papasioux; 05-09-2021 at 01:55 AM.

  2. #2
    Registered User
    Join Date
    04-27-2017
    Location
    Italy
    MS-Off Ver
    Office 2013
    Posts
    4

    Re: Run Macro when saving the workbook

    Hi papasioux,
    it's unclear what you mean by updating references on file A, you may need to adapt the code where indicated.
    Try this code, in ThisWorkbook Module (not standard module) of file B:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        
        Workbooks.Open Filename:="E:\CV3\A.xlsm"
        
        With ActiveWorkbook
            
            .Sheets(1).Calculate        '====> adapt the code according to the operation you need to perform on file A
            
        End With
        
        Dim FileToClose As String
        
        FileToClose = "E:\CV3\A.xlsm"
        
        Workbooks(Dir(FileToClose)).Close SaveChanges:=True
        
    End Sub

  3. #3
    Registered User
    Join Date
    09-25-2020
    Location
    vienna
    MS-Off Ver
    365
    Posts
    19

    Re: Run Macro when saving the workbook

    Thanks Sequoyah! I meant updating references to A by external links to other books. I solved that by deactivating the confirmation in advanced options.

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,550

    Re: Run Macro when saving the workbook

    What needs to happen with the file you want opened?
    Put in the "ThisWorkbook" module.
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim wb2 As Workbook
    Set wb2 = Workbooks.Open("E:\CV3\A.xlsm")
        'Do what needs to be done in wb2 here
    wb2.Close True    '<---- save and close wb2
    End Sub
    Last edited by jolivanes; 05-08-2021 at 06:05 PM. Reason: add code

  5. #5
    Registered User
    Join Date
    09-25-2020
    Location
    vienna
    MS-Off Ver
    365
    Posts
    19

    Re: Run Macro when saving the workbook

    Thanks Jolivanes! it worked.

    I donīt need to do something specific on A once is open. I just need A to open for a while because I have information from B in A. I needed both files to be open so that the information is updated from B to A.
    Cheers!

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,550

    Re: Run Macro when saving the workbook

    Thanks for letting us know and good luck
    Have an Obstler on your health

+ 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. [SOLVED] Block from workbook saving from personal workbook macro
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 02-17-2019, 05:57 AM
  2. [SOLVED] Saving a new workbook with macro
    By alane in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-15-2013, 06:45 AM
  3. [SOLVED] Macro to Saving Workbook Sheets as Separate Macro-Enabled Workbooks
    By puppetpalace in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-03-2013, 01:58 PM
  4. [SOLVED] Saving workbook as 2003 xls then reopen macro enabled workbook
    By JPalms in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2013, 01:24 PM
  5. Renaming Active.Workbook and saving it without saving Macro
    By Djwill in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-04-2012, 03:09 AM
  6. Macro for saving workbook
    By ad9051 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-14-2011, 12:42 PM
  7. Replies: 2
    Last Post: 02-24-2011, 05:17 PM

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