简体   繁体   English

保存Excel时的公式到价值

[英]Formula to value when saving excel

I need a formula to copy and replace a cell's formula to values, when the sheet is saved. 保存工作表时,我需要一个公式来将单元格的公式复制并替换为值。

I have a =today() formula in cells C2:E2. 我在单元格C2:E2中有一个=today()公式。 It works, but the problem is when I reopen the file it will show that day, instead of showing the day the sheet was saved. 它可以工作,但是问题是当我重新打开文件时,它将显示当天,而不是显示工作表的保存日期。

You can do this quite easily with VBA: 您可以使用VBA轻松完成此操作:

ActiveSheet.Cells(Row, Column).Value = Date ActiveSheet.Cells(Row,Column).Value =日期

You can trigger a Workbook_BeforeClose event that will replace your formula by its value: 您可以触发一个Workbook_BeforeClose 事件 ,该事件将用其值替换公式:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Range("C2:E2").Value = Range("C2:E2").Value
     ThisWorkbook.Save
End Sub

This code has to be placed in ThisWorkbook object not as a Module macro. 此代码必须放置在ThisWorkbook对象中,而不是作为Module宏。

EDIT: 编辑:

Sorry I misread. 对不起,我看错了。 If you want the replacement to be done when you save your workbook then the same applies but the code is: 如果您希望在保存工作簿时完成替换,则同样适用,但代码为:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
     Range("C2:E2").Value = Range("C2:E2").Value
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM