Sheet code module question

S

Stuart

A routine runs, whereby the contents of a workbook are copied
to a new workbook. This new workbook contains one single
worksheet. It has no standard modules, and the Thisworkbook
module is empty. However, there is still code in the new sheet's
Sheet module (specifically Worksheet_SelectionChange Event
code).

I understand this cannot be deleted.

If that is the case, can I prevent it being copied into the new
worksheet in the first place?

Regards.
 
P

Patrick Molloy

Im my example, I'm copying a range called 'Database' from
a sheet called 'main' into a new workbook...

Sub CopySheet()
Dim wb As Workbook
Dim Source As Range
Dim target As Range
Set Source = _
ThisWorkbook.Sheets("main").Range("Database")
Set wb = Workbooks.Add(xlWBATWorksheet)
With wb.ActiveSheet
Set target = .Range(.Range("A1"), _
.Cells(Source.Rows.Count, Source.Columns.Count))
End With

target.Value = Source.Value

End Sub


By just copying the values from the sheet rather than the
sheet itself, we avoid copying any code that is a
attached to the sheet

HTh
Patrick Molloy
Microsoft Excel MVP
 

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