ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using a Macro from one workbook within another (https://www.excelbanter.com/excel-programming/371262-using-macro-one-workbook-within-another.html)

Rob

Using a Macro from one workbook within another
 
Hello.

Here's the scenario... I have one workbook open and I copy specific sheets
into a new woorkbook, now both have the same macros in them, what I want to
do is use woorkbook1 to switch control to workbook2 and run a macro that is
in workbook2.

For example: Workbook1 code...

Private Sub CommandButton1_Click()
'Select Tabs and Copy
Sheets(Array("Info", "Data")).Copy

'Save New Workbook
ActiveWorkbook.SaveAs Filename:="WorkBook2.xls", FileFormat:=xlNormal,
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False,
CreateBackup:=False

Windows("WorkBook2.xls").Activate

'Use Macro That's In WorkBook2
Call DoThis(WorkBook2.xls)

End Sub

Here's the Code in WorkBook2.xls:

Sub DoThis()
WorkBooks("WorkBook2.xls").Close
MsgBox("All Done Boss.")
End Sub


Any Ideas on this?

Thanks in Advance.
Rob

bigwheel

Using a Macro from one workbook within another
 
If your workbooks have the same macros in them, wouldn't it be as well to
run the macro DoThis() from workbook1?

"Rob" wrote:

Hello.

Here's the scenario... I have one workbook open and I copy specific sheets
into a new woorkbook, now both have the same macros in them, what I want to
do is use woorkbook1 to switch control to workbook2 and run a macro that is
in workbook2.

For example: Workbook1 code...

Private Sub CommandButton1_Click()
'Select Tabs and Copy
Sheets(Array("Info", "Data")).Copy

'Save New Workbook
ActiveWorkbook.SaveAs Filename:="WorkBook2.xls", FileFormat:=xlNormal,
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False,
CreateBackup:=False

Windows("WorkBook2.xls").Activate

'Use Macro That's In WorkBook2
Call DoThis(WorkBook2.xls)

End Sub

Here's the Code in WorkBook2.xls:

Sub DoThis()
WorkBooks("WorkBook2.xls").Close
MsgBox("All Done Boss.")
End Sub


Any Ideas on this?

Thanks in Advance.
Rob



All times are GMT +1. The time now is 01:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com