Copying Code

R

Roger

Could some one please give me an example of the code required to copy all the
code from 1 module into another module in the same workbook

Thankyou in anticipation
 
B

Bernie Deitrick

Roger,

Not sure WHY you would want to do that - but set a reference to MS VBA extensibility, and use code
like

Sub CopyModule()
Dim NextLine As Integer
Dim theCode As String

With ActiveWorkbook.VBProject.VBComponents.Item("Module1").CodeModule
theCode = .Lines(1, .CountOfLines)
End With
ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule).Name = "NewModule1"
With ActiveWorkbook.VBProject.VBComponents.Item("NewModule1").CodeModule
.DeleteLines 1, .CountOfLines
NextLine = .CountOfLines + 1
.InsertLines NextLine, theCode
End With
End Sub

HTH,
Bernie
MS Excel MVP
 
R

Rick Rothstein

Why would you just use Windows Copy/Paste to do that? Just insert a new
module, select all the text in your existing module and Edit/Copy it, put
the cursor in your new module and then Edit/Paste it in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top