Solved

Copying Workbook module together with worksheet module

Posted on 2013-11-27
9
282 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now