• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

Copying Workbook module together with worksheet module

I am copying a worksheet to a new workbook. This copies the workbook module also. I want to also copy the workbook module to the new workbook. Is this possible? Is it also possible to copy a standard module using VBA?
0
Saqib Husain, Syed
Asked:
Saqib Husain, Syed
  • 2
  • 2
  • 2
  • +2
1 Solution
 
Saqib Husain, SyedEngineerAuthor Commented:
Oops!!! Is that what I did?
0
 
FaustulusCommented:
As a general rule, it isn't recommended to grant access to code on your computer to VBA because if your code can access VBA projects so can malicious intruders' code. For this reason access to VBA projects can only be granted manually. Microsoft has not provided for a way to open that door using code. You may have seen the checkbox for "Allow access to VBA Project" on one of the Security dialog boxes. That's the one.
Once you grant access to VBA projects you can write (and execute) code to write code and, of course, to copy any code from any project to any other project on your computer. Other than that you can copy a workbook's code to another workbook only by making a copy of that book or, as you have pointed out, copy a worksheet's code along with the rest of that sheet.
0
 
andrewssd3Commented:
In principle I agree with Faustulus, but sometimes there is a legitimate need to do the sort of thing ssaqibh is asking as long as you are careful.  If you want to do this, here is some sample code that copies the contents of the ThisWorkbook module (assuming this is what you mean) to a new workbook. You will need a reference to Microsoft Visual Basic for Applications Extensibility 5.3
Public Sub MoveModule()

    Dim cmOld As CodeModule
    Dim cmNew As CodeModule
    
    Dim wbkNew As Workbook
    
    Dim s As String
    
    Set cmOld = ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    
    s = cmOld.Lines(1, cmOld.CountOfLines)
    
    Set wbkNew = Application.Workbooks.Add
    
    Set cmNew = wbkNew.VBProject.VBComponents("ThisWorkbook").CodeModule
    
    cmNew.DeleteLines 1, cmNew.CountOfLines
    cmNew.AddFromString s
    
    
End Sub

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
SeanStricklandCommented:
Agreed with both of the prior answers.  Good tip on using caution, and I've used code very similar to Andrew's before for a legitimate purpose.

Watch out for duplicate declarations, etc. in the event your public functions/procedures have the same name across workbooks.

Flagging for my knowledge-base -- this is definitely useful. :)

Sean
0
 
broro183Commented:
This is not for points. Chip Pearson has an informative page which re-iterates some of the points in the earlier posts & has a range of examples:http://www.cpearson.com/excel/vbe.aspx

hth
Rob
0
 
andrewssd3Commented:
Hi Saqib - did any of our responses give you what you were looking for?
0
 
FaustulusCommented:
Ssaqibh,
I think the easiest way to achieve what you wish to do is to create a copy of the entire workbook under another name and delete all those worksheets which you don't need. This can be done without accessing the VBA project and requires only simple code.
0
 
Saqib Husain, SyedEngineerAuthor Commented:
I wonder why I missed this from your first statement. This is a very simple solution.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now