在保存Excel文件时,创建一个新的工作簿并复制两个工作表,将这两个工作表中的公式删除,保存为新的工作簿。以下是代码示例:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
If Success Then
Dim newBook As Workbook
Dim copySheet1 As Worksheet
Dim copySheet2 As Worksheet
Set newBook = Workbooks.Add
' 复制工作表1,去掉公式
Set copySheet1 = ThisWorkbook.Worksheets("Sheet1")
copySheet1.Copy Before:=newBook.Sheets(1)
With newBook.Sheets(1).UsedRange
.Formula = .Value
End With
' 复制工作表2,去掉公式
Set copySheet2 = ThisWorkbook.Worksheets("Sheet2")
copySheet2.Copy Before:=newBook.Sheets(2)
With newBook.Sheets(2).UsedRange
.Formula = .Value
End With
' 保存新工作簿
newBook.SaveAs "NewWorkbook.xlsx"
newBook.Close
End If
End Sub
上一篇:AfterReturningAdvice是在事务关闭之前还是之后启动的?
下一篇:AftersavingparententityIamlosingchildentitydataevenIsentchilddatainpayload