Sub values_dump()
Dim sourceWB As Workbook
Dim destWB As Workbook
Dim ws As Worksheet
Dim path As String
Dim fname As String
Application.ScreenUpdating = False
path = ThisWorkbook.path & "\_bck\"
fname = "values_" & Format(Now, "dd_mmm_yy_hh_mm_ss") & ".xlsm"
Set sourceWB = ThisWorkbook
Set destWB = Workbooks.Add
destWB.SaveAs path & fname
For Each ws In sourceWB.Worksheets
Workbooks(sourceWB).Sheets(ws).Copy after:=Workbooks(destWB).Sheets(1)
Next ws
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
我在这一行destWB.SaveAs path & fname
收到错误 - 它说我不能使用" .xlsm"延期?
此外,我想将工作表复制到新工作簿,但只保留值和原始格式。
我的代码错误地复制了所有公式。我不想以任何方式破坏原始工作簿。
答案 0 :(得分:1)
您正在任意处理启用宏的工作簿文件扩展名(例如xlsm),但使用Workbook.SaveAs method和默认的 FileFormat 参数(在Excel选项中找到►保存►在此保存文件)格式:事实上,最好完全不使用.xlsm并指定所需的文件格式。如果选择正确的格式,Excel将添加.xlsm。有关可用的SaveAs文件的完整列表,请参阅xlFileFormat enumeration类型。
如果要将公式恢复为其值,只需复制工作表,然后使用.Cells = .Cells.Value
。
Sub values_dump()
Dim sourceWB As Workbook
Dim destWB As Workbook
Dim ws As Worksheet
Dim path As String
Dim fname As String
Dim c As long
Application.ScreenUpdating = False
path = ThisWorkbook.path & "\_bck\"
fname = "values_" & Format(Now, "dd_mmm_yy_hh_mm_ss") & ".xlsm"
Set sourceWB = ThisWorkbook
Set destWB = Workbooks.Add
destWB.SaveAs Filename:=path & fname, FileFormat:=xlOpenXMLWorkbookMacroEnabled 'Open XML Workbook Macro Enabled (52)
For Each ws In sourceWB.Worksheets
if ws.autofiltermode then ws.autofiltermode = false
ws.Copy after:=destWB.Sheets(1)
With destWB.Sheets(2).usedrange
for c = 1 to .columns.count
.columns(c).Cells = .columns(c).Cells.Value
next c
End With
destWB.save
Next ws
Application.ScreenUpdating = True
End Sub
当您将工作簿类型var设置为Workbook Object时,您可以直接使用var。你好像在使用Workbook.Name property。 Worksheet Object和工作表.Name property也是如此。