Is it possible?

atf32

Board Regular
Joined
Apr 13, 2011
Messages
157
OS: Windows 7
MS Excel 2010

I have a file that saves out some of its worksheets to an external file and then saves that file as its own file. Code from the copied out sheets also copy out to the new file....Question: is is possible to save/export the code from the "ThisWorkbook" object from the VBE to the new file? :confused:
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Actually, I described this wrong.......I want to know how to export a copy of the "ThisWorkbook.cls" to the new file.
 
Upvote 0
ActiveWorkbook.SaveAs Filename:="C:\Users\user\Desktop\Book1.xls", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

Try this code......Muz:)
 
Upvote 0
Hi atf32,

Yes, it is possible. Just as an example, here is code that will create a new workbook and copy the ThisWorkbook code from the workbook containing the code to the new workbook:

Code:
Sub CodeCopy()

   'Macro to create a new sheet and copy the macro module
   'from sheet1 to it.  Must install Microsoft Visual Basic
   'for Applications Extensibility library from Tools > References.
   
   Dim i          As Integer
   Dim NewWB      As Workbook
   Dim SrcCmod    As VBIDE.CodeModule
   Dim DstCmod    As VBIDE.CodeModule
   
   Set NewWB = Workbooks.Add

   Set SrcCmod = ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
   Set DstCmod = NewWB.VBProject.VBComponents("ThisWorkbook").CodeModule
   
   For i = 1 To SrcCmod.CountOfLines
      DstCmod.InsertLines i, SrcCmod.Lines(i, 1)
   Next i
   
End Sub

Please note that you must install Microsoft Visual Basic for Applications Extensibility library from Tools > References in the VBE.
 
Upvote 0
from the Visual Basic Editor, choose File > Export
to save the code as a text file
I often save modules that I might want to use somewhere else this way

then when you want to use it in another workbook, go to the VBE and do File, Import

didn't see Damon's answer when I wrote this -- his way is much more elegant ;)
 
Upvote 0
Thanks, but this won't work....My file copies a number of tabs, which creates a default secondary file. My code then renames the seconadry file from the default filename......This all works fine. However, now I want to copy code that resides in the "ThisWorkbook.cls" (VBE). So that a "Workbook_BeforeClose" event executes in the new file.
 
Upvote 0
Hi atf32,

Yes, it is possible. Just as an example, here is code that will create a new workbook and copy the ThisWorkbook code from the workbook containing the code to the new workbook:

Code:
Sub CodeCopy()

   'Macro to create a new sheet and copy the macro module
   'from sheet1 to it.  Must install Microsoft Visual Basic
   'for Applications Extensibility library from Tools > References.
   
   Dim i          As Integer
   Dim NewWB      As Workbook
   Dim SrcCmod    As VBIDE.CodeModule
   Dim DstCmod    As VBIDE.CodeModule
   
   Set NewWB = Workbooks.Add

   Set SrcCmod = ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
   Set DstCmod = NewWB.VBProject.VBComponents("ThisWorkbook").CodeModule
   
   For i = 1 To SrcCmod.CountOfLines
      DstCmod.InsertLines i, SrcCmod.Lines(i, 1)
   Next i
   
End Sub

Please note that you must install Microsoft Visual Basic for Applications Extensibility library from Tools > References in the VBE.

OMG......Your good.....;o) This worked great!!!. Thanks.​
 
Upvote 0
This is a similar approach.
This does not require the Microsoft Visual Basic for Applications Extensibility library be open.

Code:
Sub test()
    Dim codeToWrite As String
    Dim newWorkbook As Workbook
    
    Set newWorkbook = Workbooks.Add
    
    With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
        codeToWrite = .Lines(1, .CountOfLines)
    End With
    
    With newWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
        .DeleteLines 1, .CountOfLines
        .AddFromString codeToWrite
    End With
End Sub

The .DeleteLines is to prevent double Option Explicit lines if your computer is configured to automatically insert them.
 
Upvote 0

Forum statistics

Threads
1,221,539
Messages
6,160,412
Members
451,644
Latest member
hglymph

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top