Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help to copy macro to another workbook.

Status
Not open for further replies.

cg084

Technical User
May 3, 2002
67
GB
Hello,

I am trying to create a macro that will copy a different macro in workbook1 to a named sheet in another workbook.

e.g

Workbook 1
Contains Sheet1
Contains MacroA

Workbook2
Contains Sheet1


I need to write a macro that will copy Workbook 1 Sheet1 MacroA and place it in Workbook2 Sheet1.

Thank you for any help.
CG084
 
I am very sure the much smarter Excel gurus here can do it better, but the following does work. The VBProjects in the different workbooks are explicitly named - vbaprojectbook1, and vbaprojectbook2. It copys the code in Book1(ThisWorkbook) to Book2(ThisWorkbook).

Code:
Application.VBE.VBProjects("vbaprojectbook1"). _
    VBComponents.Item("ThisWorkbook").Export "c:\test.bas"
Application.VBE.VBProjects("vbaprojectbook2"). _
    VBComponents.Item("ThisWorkbook").CodeModule.AddFromFile "c:\test.bas"

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top