?
Solved

Copying Workbook module together with worksheet module

Posted on 2013-11-27
9
Medium Priority
?
326 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

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.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

765 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