Solved

Copying Workbook module together with worksheet module

Posted on 2013-11-27
9
311 Views
Last Modified: 2013-12-10
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
Comment
Question by:Saqib Husain, Syed
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 39685948
Oops!!! Is that what I did?
0
 
LVL 14

Expert Comment

by:Faustulus
ID: 39685971
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
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39686133
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:SeanStrickland
ID: 39686397
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
 
LVL 10

Expert Comment

by:broro183
ID: 39686460
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
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39704772
Hi Saqib - did any of our responses give you what you were looking for?
0
 
LVL 14

Accepted Solution

by:
Faustulus earned 500 total points
ID: 39708007
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
 
LVL 43

Author Closing Comment

by:Saqib Husain, Syed
ID: 39708257
I wonder why I missed this from your first statement. This is a very simple solution.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question