简体   繁体   English

将工作表复制/粘贴到没有该工作表的 VBA 代码的新工作簿

[英]Copy/Paste worksheet to new workbook without that worksheet's VBA code

What I want to do is to create a normal Excel workbook (.xlsx) where I summarize the inputs/outputs of some simulations by copying some sheets from a Macro-enabled workbook (.xlsm) and pasting in a normal Excel workbook.我想要做的是创建一个普通的 Excel 工作簿 (.xlsx),通过从启用宏的工作簿 (.xlsm) 复制一些工作表并粘贴到一个普通的 Excel 工作簿中,我总结了一些模拟的输入/输出。 The original sheets have macros, shapes, named ranges, dropdown lists, and some formatting.原始工作表具有宏、形状、命名范围、下拉列表和一些格式。 I want some properties of the sheets to come over to the new workbook (named ranges, row and column formatting, cell formatting) but not others (shapes, dropdown lists, macros).我希望工作表的某些属性适用于新工作簿(命名范围、行和列格式、单元格格式)而不是其他属性(形状、下拉列表、宏)。 I also have to distribute the .xlsm workbook to other users, so I want a solution that doesn't require the user to grant permissions.我还必须将 .xlsm 工作簿分发给其他用户,因此我想要一个不需要用户授予权限的解决方案。

If I copy/paste like below, then I get all the properties of the worksheet to come over to the new workbook.如果我像下面这样复制/粘贴,那么我将获得工作表的所有属性以转到新工作簿。 I have figured out how to delete shapes and remove dropdown list formatting from the sheet in the new workbook, but I can't remove the worksheet's VBA code without modifying the VBA references.我已经想出了如何从新工作簿中的工作表中删除形状和下拉列表格式,但我无法在不修改 VBA 引用的情况下删除工作表的 VBA 代码。

If I paste special, then I can avoid bringing over the worksheet's VBA code, but I can't bring over the named ranges.如果我粘贴特殊,那么我可以避免引入工作表的 VBA 代码,但我不能引入命名范围。

CopySheetToWB(sht as string, wb_New as workbook)

    Dim sht_Name as string, rng As Range, shp as shape
    Dim ws As Worksheet, wb As Workbook, ws_New As Worksheet

    ' set sheet in CURRENT wb
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets(sht)

    ' copy/paste sheet to NEW wb
    ws.Copy after:=wb_New.Sheets(wb_New.Sheets.Count)

    ' delete shapes from NEW ws
    Set ws_New = wb_New.Worksheets(sht)
    For Each shp In ws_New.Shapes
        shp.Delete
    Next shp

    ' remove dropdown lists from copied sheet
    ws_New.Cells.Validation.Delete

End Sub

The answer to my problem turned out to be quite simple.我的问题的答案很简单。 My old code that wasn't working and my new code that is working.我的旧代码无效,而我的新代码有效。 If you save the file type correctly, then the macros in the worksheets are not a problem as they are removed.如果您正确保存了文件类型,则工作表中的宏不会因为它们被删除而造成问题。

wb_New.SaveAs FileName:=str_fName, FileFormat:=xlWorkbookNormal   'old code
wb_New.SaveAs FileName:=str_fName, FileFormat:=51                 'new code

Also make sure to include the file extension in str_fName.还要确保在 str_fName 中包含文件扩展名。 VBA doesn't automatically append the correct file extension based on the FileFormat you choose. VBA 不会根据您选择的 FileFormat 自动附加正确的文件扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA复制并粘贴到新工作表 - VBA Copy and Paste to new Worksheet 改进 VBA 代码将工作表复制到新工作簿并保存为 CSV 文件 - Improve VBA code to copy a worksheet to a new workbook and save it as CSV file Excel 将当前活动工作表复制到新工作簿 VBA 代码 - Excel copy current active worksheet to new workbook VBA Code VBA(Excel)将工作表复制到新工作簿 - VBA (excel) copy worksheet to new workbook VBA 将多个工作表复制到另一个工作簿的代码 - VBA Code to copy multiple worksheet to another workbook VBA 复制粘贴到新工作表中,如果语句和转置 - VBA Copy Paste into new worksheet, If statement and transpose VBA动态筛选和复制粘贴到新工作表中 - VBA Dynamic Filtering and Copy Paste into new worksheet 将工作表复制到新工作簿中 - Copy a Worksheet into a new Workbook 在单独的工作簿中具有宏以查找工作簿,将其作为值从工作表中复制并粘贴到新工作簿中,并保存在原始工作簿的位置 - Have macro in separate workbook to locate workbook, copy and paste as values from worksheet into new workbook and save in original workbook's location VBA:如果工作簿中的工作表名称等于从用户窗体中选择的组合框值,则复制该工作表并将其粘贴到另一个工作簿中 - VBA: If Worksheet name in Workbook equals Combo Box value selected from Userform then copy that Worksheet and paste it into another Workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM