PDA

View Full Version : Open And Activate Workbook Before Runing Macro



Howardc
06-04-2013, 02:18 PM
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




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



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

Excel Fox
06-04-2013, 02:30 PM
Try passing the workbook by reference on to the routine that does the work, instead of relying on 'ActiveWorkbook'

snb
06-04-2013, 03:41 PM
If each workbook contains an 'auto' macro this will suffice (since an 'auto' macro runs automatically when opening the file...)

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.

Howardc
06-04-2013, 03:59 PM
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

snb
06-04-2013, 06:17 PM
You can use the

ThisWorkbook.UpdateLinks = 3
property in each workbook. No macro required.

Howardc
06-04-2013, 07:23 PM
Thanks for the help, much appreciated