Solved

Copying Workbook module together with worksheet module

Posted on 2013-11-27
9
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

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.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

734 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