Excel 工作表页脚中的上次保存日期
我正在将 Excel 2007 与包含许多工作表的工作簿一起使用。我需要在页脚中包含上次保存工作表的日期。我能够找到以下内容:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws.PageSetup
.LeftFooter = "Last Save Time: &T"
.RightFooter = "Last Save Date: &D"
End With
Next ws
Set ws = Nothing
End Sub
这会更改每个工作表。我需要它只更改已编辑的工作表(因此每个工作表可以有不同的日期)。 这可能吗?我应该使用单元格而不是页脚吗?或者我必须创建多个工作簿吗?
谢谢!
I am using Excel 2007 with a workbook that has many sheets. I need to have the date when the worksheet was last saved - in the footer. I was able to find the following:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws.PageSetup
.LeftFooter = "Last Save Time: &T"
.RightFooter = "Last Save Date: &D"
End With
Next ws
Set ws = Nothing
End Sub
This changes every worksheet. I need it to only change a sheet that has been edited (so each worksheet can have a different date).
Is this even possible? Should I use a cell instead of a footer? Or do I have to create multiple workbooks?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Remnant 所建议的,最好的方法是利用
Worksheet_Change
事件。 需要此 VBA 代码这意味着,您的每个工作表中都 。仅当您更改单元格内容时才会引发此事件,而不是在更改选择时引发,因此它可能是您想要的。
Remnant 还写道,如果您有很多工作表,这将是“设置起来很痛苦”。我认为这取决于你所说的“很多”。对于最多 20~40 个工作表,可以在几分钟内轻松地将上面的代码手动复制到每张工作表中。
如果您已经有一个工作簿和很多(例如,超过 40 张)工作表,那么以编程方式添加代码可能是个好主意。将包含此代码的单独模块添加到您的工作簿中,并运行一次:
这会将上面的“Worksheet_Change”事件添加到每个工作表的代码部分。
As Remnant suggested, best approach would be to utilize the
Worksheet_Change
event. That means, you need this VBA codein every of your worksheets. This event is only raised when you change a cells content, not when you change the selection, so it may be what you want.
Remnant also wrote that this would be a "pain to set up" if you have many worksheets. I think that depends on what you call "many". For up to 20~40 worksheets, the code from above can be easily copied manually to every sheet in a few minutes.
If you already have a workbook and a lot (say, more that 40) sheets, then it may be a good idea to add the code programmatically. Add a separate module to your workbook, containing this code, and run it once:
This will add the "Worksheet_Change" event from above to the code section of every sheet.
它修改每个工作表的原因是因为您正在执行“For Each WS in ThisWorkbook.Worksheets”。将其更改为仅查找您要修改的工作表,然后您就可以开始了。
例如,如果您没有要修改的工作表的引用,请保留 For Each 循环,但不要修改每个工作表,而是检查工作表以查看它是否是您想要的工作表。如果是,请修改页脚。否则,忽略它。
The reason it modifies every worksheet is because you are doing a "For Each WS in ThisWorkbook.Worksheets". Change that to only find the worksheet you want to modify and you will be good to go.
For example, if you don't have a reference to the worksheet you want to modify, keep the For Each loop but instead of modifying each one, do a check on the sheet to see if it is the one you want. If it is, modify the footer. Otherwise, ignore it.
一种选择可能是每次更改工作表时更新页脚。这意味着各个工作表将具有不同的日期/时间,具体取决于更新时间。
然后用户可以正常保存工作簿。
这能满足您的需要吗?
One option might be to update the footers each time a sheet is changed. This will mean that individual sheets will have different dates / times dependent upon when they were updated.
The user can then save the workbook as normal.
Does this gie you what you need?