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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel97 - Copy Code Between Workbooks

Status
Not open for further replies.

SHardy

Programmer
May 9, 2001
231
GB
I want to write some code that copies a VBA module (or just a procedure) from one open workbook to another.
It is important that no other code is present in the target workbook, other than the module to be copied.

How can I do this?

Thanks in advance.
 
Well, for one,the code you have will work in any workbook. Just make reference to the proper workbooks, sheets, ranges, etc and you can manipulate any spreadsheet from any other spreadsheet.

If you must copy it, use this:

Code:
   Sub CopyModule()
      Dim CodeLines As String
      Dim ModuleToCopy As VBComponent
      Dim NewModule As VBComponent

      ' Set a variable to the module to copy.
      ' Note: This assumes that the name of the module to copy is
      ' "module2". Replace "Module2" with the name of the module to copy.
      Set ModuleToCopy = _
             Application.VBE.ActiveVBProject.VBComponents("module2")

      ' Get the Visual Basic code from the module.
      CodeLines = ModuleToCopy.CodeModule.Lines _
                 (1, ModuleToCopy.CodeModule.CountOfLines)

      ' Create a module sheet in another workbook.
      ' Note: You must refer to the name of the Visual Basic project. By
      ' default, the name of all new projects is "VBAProject". You may want
      ' to rename the project to which you are copying the contents of the
      ' module sheet.
      Set NewModule = Application.VBE.VBProjects("VBAProject") _
                      .VBComponents.Add(vbext_ct_StdModule)

      ' Add the Visual Basic code to the new module.
      NewModule.CodeModule.AddFromString CodeLines

      ' Rename the new module to the name of the previous module.
      NewModule.Name = ModuleToCopy.Name

   End Sub
Tyrone Lumley
augerinn@gte.net
 
Thanks for your response.

Yes I am aware that you can reference code within another workbook, but it is not applicable in this case.

I have a financial reporting system setup within Excel. This is used in a number of overseas offices. Each week, these offices will run a function to create a copy to return to the UK office. It is currently setup so that this copy contains no formulas or code. However, it is now required that the copy keeps the code fro the print function only. There fore my question was so that I can copy the print code across to the copy file.

I have tried using your code. I added the first two lines when the source workbook is active, and the last three lines when the target workbook is active. However, I am getting the following error:

********************************************************
Run-time error '32813'
Application-defined or object-defined error
********************************************************

Any ideas why this may be?

For information, I have also been given an alternative method, as follows:

Workbooks("Source.xls").VBProject.VBComponents("Module1").Export "c:\code.bas"
Workbooks("Target.xls").VBProject.VBComponents.Import("c:\code.bas").Name="Module1"
Kill ("c:\code.bas")

I have tested this, and it gives the desired result, but I would still be interested in getting your suggestion working.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top