Copy Worksheet without its code

stemar

Board Regular
Joined
Mar 16, 2002
Messages
248
I need to use VBE to copy a worksheet from an existing workbook into a new one. The original worksheet has a private function used for data validation which I don't want in the new workbook.

I'd prefer not to modify the VBProject object directly as it'll be running under XP and used on several different computers by users who shouldn't be let anywhere near the security settings, and these won't be set to allow VBE to modify VBProject components.

Is there an easy way around this?

Thanks,

Steve
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I believe this will do what you want:

ActiveSheet.Copy
With ActiveWorkbook
.SaveAs "NewFileName.xls", FileFormat:=xlNormal
End With

Hope that helps,

Smitty
 
Upvote 0
No, I thought that too, but it you try it, it will copy the worksheet module with the code in it too... which, well, I don't know. I guess it depends on how the OP has it set up !
 
Upvote 0
Weird,

I have a file with a bunch of code in it and none of the individual sheet or ThisWorkbook code copy with that method.

I don't think I've done anything specific to prevent copying.

Smitty
 
Upvote 0
Perhaps this might work. If not please post back.

Sub Test2()
Dim wkb As Workbook
Set wkb = Workbooks.Add
ThisWorkbook.Worksheets("Sheet1").Copy after:=wkb.Worksheets(wkb.Worksheets.count)
With ActiveSheet
With .Parent.VBProject.VBComponents(.CodeName)
.CodeModule.DeleteLines 1, .CodeModule.CountOfLines
End With
End With
End Sub
 
Upvote 0
Tom, as the OP said, that is very likely to fail in XP because of the new security setting, that is checked by default, that disables access to the VB Project.
 
Upvote 0
Thanks to all.

I've gone with the workbooks.add and copy method.

It needed the following steps:

Turn calculation off (I have custom functions in there, just to complicate matters!)

Copy the entire original worksheet

Paste into new wkbook (Copies formatting and formulas)

Copy the new worksheet

PasteSpecial values (replaces formulas that refer to other worksheets with values)

Turn calculation back on
 
Upvote 0
Hey JPG, yep I skipped over that part of the OP's request.

I generally like XP but the logic of that new feature escapes me in cases like this - - where you need to ask the user to give you access to the project to prevent them from accessing the code in the project. Go figure.
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,917
Members
449,055
Latest member
KB13

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top