Results 1 to 6 of 6

Thread: Open And Activate Workbook Before Runing Macro

  1. #1
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13

    Open And Activate Workbook Before Runing Macro

    I have set up the following code to open open workbooks and to activate the macro

    This is just a sample of what I want to set up as I have several branches. The code vworks perfectly. However, when selecting the macro update_workbooks. I would like to first like to select/see the name of the workbook that is going to be updated by this macro so that I can which file to choose from the downloaded files

    Your assistance in resolving this is most appreciated



    HTML Code:
    Sub Open_Workbooks()
        ChDir ("C:\My documents")
        Application.ScreenUpdating = False
        Workbooks.Open Filename:="C:\My Documents\BR1 parts sales.xlsm"
        Workbooks.Open Filename:="C:\My Documents\Br1 service sales.xlsm"
       
        Windows("Open Parts & Service Sales account.xls").Activate
    End Sub

    HTML Code:
    Sub Update_Workbooks()
    
        Application.DisplayAlerts = False
            Windows("BR1 parts sales.xlsm").Activate
       Application.Run "'Br1 Parts Sales.xlsm'!Auto"
        ActiveWorkbook.Close
        Windows("Br1 Service sales.xlsm").Activate
        Application.Run "'BR1 service Sales.xlsm'!Auto"
        ActiveWorkbook.Close
    End Sub

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Try passing the workbook by reference on to the routine that does the work, instead of relying on 'ActiveWorkbook'
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    If each workbook contains an 'auto' macro this will suffice (since an 'auto' macro runs automatically when opening the file...)
    Code:
    Sub M_snb()
        with getobject("C:\My Documents\BR1 parts sales.xlsm")
            .close true
        end with
        with Getobject("C:\My Documents\Br1 service sales.xlsm")
           .close true
        end with
    End Sub
    PS. Avoid 'Select' and 'Activate' in VBA.
    Last edited by snb; 06-04-2013 at 03:44 PM.

  4. #4
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Thanks for the reply. I have 30 workbooks that needs to be updated from files that are downloaded. What I would like to see is the workbook name before the update macro is run. i.e have a pause button so that one can see the workbook before the update macro is run for a particular workbook

  5. #5
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    13
    You can use the
    Code:
    ThisWorkbook.UpdateLinks = 3
    property in each workbook. No macro required.

  6. #6
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Thanks for the help, much appreciated

Similar Threads

  1. Excel VBA Macro To Open A File Through Browse Dialog Box
    By Safal Shrestha in forum Excel Help
    Replies: 2
    Last Post: 04-05-2013, 12:59 PM
  2. VBA Code to Open Workbook and copy data
    By Howardc in forum Excel Help
    Replies: 16
    Last Post: 08-15-2012, 06:58 PM
  3. Replies: 8
    Last Post: 08-05-2012, 10:07 AM
  4. Get Name List of All Open Workbook Files
    By princ_wns in forum Excel Help
    Replies: 5
    Last Post: 04-07-2012, 12:18 PM
  5. Assign an event to chart on workbook open
    By LalitPandey87 in forum Excel Help
    Replies: 2
    Last Post: 02-20-2012, 07:43 AM

Posting Permissions

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