问题
I can copy a worksheet by calling its .Copy method.
Sheets("Example").Copy After:=Worksheets("Sheet3")
However, this also copies any macros or event handlers associated with that worksheet. How do I copy the worksheet without copying any Visual Basic code?
回答1:
After copying the sheet, you can reference it by name and then delete all of the lines from the code module:
Sheets("Example").Copy After:=Sheets("Sheet3")
' Get the code/object name of the new sheet...
Dim strObjectName As String
strObjectName = ActiveSheet.CodeName
' Remove all lines from its code module...
With ThisWorkbook.VBProject.VBComponents(strObjectName).CodeModule
.DeleteLines 1, .CountOfLines
End With
To work with project components, you'll need to ensure that the "Trust access to the VBA project object model" option is enabled in Excel's macro settings.
回答2:
Create an empty worksheet and copy the contents of the original sheet over.
Sub Copy_Sheet_Without_Code(original_sheet As String, copied_sheet As String)
' Creates a copy of original_sheet without any VBA code associated with it
' The copy is named copied_sheet
Sheets(original_sheet).Cells.Copy
Sheets.Add.Name = copied_sheet
Sheets(copied_sheet).Cells.PasteSpecial Paste:=xlPasteAll
End Sub
回答3:
Not exactly what the OP wanted, but you can also remove the worksheet macros by copying the worksheet to a new workbook, and then saving that workbook using the following code (which ultimately saves the workbook as .xlsx and strips the code away) ...
ActiveWorkbook.SaveAs fileName:="yourfile.xlsx", fileFormat:=51
This has the benefit of persisting all the data and formatting (eg column widths), and doesn't require VBA object model permissions.
Of course, once saved, you could then reopen the saved workbook and move the sheet back into the original workbook, now without the code!
回答4:
I tried to use the code in one of the answers before
ActiveWorkbook.SaveAs fileName:="yourfile.xlsx", fileFormat:=51
but the program displayed alert and in this case user must manually react to this alert, so I added additional code lines:
Application.DisplayAlerts = False
With ActiveWorkbook
.SaveAs Filename:=excelReportFilePath, FileFormat:=xlOpenXMLWorkbook ' this enumeration value is the same as 51
End With
Application.DisplayAlerts = True
In this case, code block worked without alert and after formed workbook with copied sheets in which originally were code was closed, and then opened again from its directory, in the copied sheets the code is absent. In this case alert - Programmatic access to Visual Basic Project is not trusted - wasn't thrown as can be on some user PC as in the case when code
' Remove all lines from its code module...
With ThisWorkbook.VBProject.VBComponents(strObjectName).CodeModule
.DeleteLines 1, .CountOfLines
End With
was used. So the advantage of this way that you don't need to care about security settings in a user PC and about correct copying data from source sheet into the additionally created empty sheet.
来源:https://stackoverflow.com/questions/32016718/copy-a-worksheet-without-copying-the-code