vba: Copy worksheet including named ranges not including macros

urungus

New Member
Joined
Mar 4, 2009
Messages
4
Is there a way to programatically copy a worksheet from one workbook to another in Excel 2003 in the following way, without having to activate or install any additional code modules (eg VBA Extensibililty library):

copy values, formats, named ranges
do not copy macros or buttons embedded in the source sheet

If I use vbas worksheet.copy function, it includes the macros, which are hard to get rid of

If I do a worksheet.cells.copy, it doesn't copy the named ranges.

Thanks for any advice.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I think the easiest solution would be to get rid of the WorkSheet code after the copy.

This code deletes all code from the active worksheet module.
Code:
Sub DeleteSheetModuleCode()
Dim x As Integer
Dim Proceed As VbMsgBoxResult
Dim Prompt As String
Dim Title As String

Prompt = "Are you certain that you want to delete" & vbCrLf _
& "all Code from " & ActiveSheet.Name & " Module?"
Title = "Verify Procedure"

Proceed = MsgBox(Prompt, vbYesNo + vbQuestion, Title)
If Proceed = vbNo Then
MsgBox "Procedure Canceled", vbInformation, "Procedure Aborted"
Exit Sub
End If

On Error Resume Next

With ActiveWorkbook.VBProject
    For x = .VBComponents.Count To 1 Step -1
    If Not .VBComponents(x).CodeModule.Name = "Sheet2" Then
        .VBComponents(x).CodeModule.DeleteLines _
        1, .VBComponents(x).CodeModule.CountOfLines
    End If
    Next x
End With
On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,542
Messages
6,056,012
Members
444,840
Latest member
RazzelDazel

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