Excel 工作表页脚中的上次保存日期

发布于 2024-11-06 00:47:47 字数 474 浏览 5 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜光 2024-11-13 00:47:47

正如 Remnant 所建议的,最好的方法是利用 Worksheet_Change 事件。 需要此 VBA 代码

Private Sub Worksheet_Change(ByVal Target As Range)
    With Me.PageSetup
        .LeftFooter = "Last Change Time: " & Format(VBA.Time, "hh:mm:ss")
        .RightFooter = "Last Change Date: " & Format(VBA.Date, "dd/mm/yy")
    End With
End Sub

这意味着,您的每个工作表中都 。仅当您更改单元格内容时才会引发此事件,而不是在更改选择时引发,因此它可能是您想要的。

Remnant 还写道,如果您有很多工作表,这将是“设置起来很痛苦”。我认为这取决于你所说的“很多”。对于最多 20~40 个工作表,可以在几分钟内轻松地将上面的代码手动复制到每张工作表中。

如果您已经有一个工作簿和很多(例如,超过 40 张)工作表,那么以编程方式添加代码可能是个好主意。将包含此代码的单独模块添加到您的工作簿中,并运行一次:

Option Explicit
Sub InsertCode()
    Dim VBProj As Object
    Dim VBComp As Object
    Dim CodeMod As Object
    Dim sh As Worksheet
    Dim LineNum As Long

    Set VBProj = ActiveWorkbook.VBProject
    For Each sh In ThisWorkbook.Sheets
        Set VBComp = VBProj.vbcomponents(sh.Name)
        Set CodeMod = VBComp.CodeModule
        With CodeMod
            .DeleteLines 1, .CountOfLines
            LineNum = .CreateEventProc("Change", "Worksheet")
            LineNum = LineNum + 1
            .InsertLines LineNum, "Me.Pagesetup.LeftFooter = ""Last Change Time: "" & Format(VBA.Time, ""hh:mm:ss"")"
            .InsertLines LineNum + 1, "Me.Pagesetup.RightFooter = ""Last Change Date: "" & Format(VBA.Date, ""dd/mm/yy"")"
        End With
    Next
End Sub

这会将上面的“Worksheet_Change”事件添加到每个工作表的代码部分。

As Remnant suggested, best approach would be to utilize the Worksheet_Change event. That means, you need this VBA code

Private Sub Worksheet_Change(ByVal Target As Range)
    With Me.PageSetup
        .LeftFooter = "Last Change Time: " & Format(VBA.Time, "hh:mm:ss")
        .RightFooter = "Last Change Date: " & Format(VBA.Date, "dd/mm/yy")
    End With
End Sub

in 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:

Option Explicit
Sub InsertCode()
    Dim VBProj As Object
    Dim VBComp As Object
    Dim CodeMod As Object
    Dim sh As Worksheet
    Dim LineNum As Long

    Set VBProj = ActiveWorkbook.VBProject
    For Each sh In ThisWorkbook.Sheets
        Set VBComp = VBProj.vbcomponents(sh.Name)
        Set CodeMod = VBComp.CodeModule
        With CodeMod
            .DeleteLines 1, .CountOfLines
            LineNum = .CreateEventProc("Change", "Worksheet")
            LineNum = LineNum + 1
            .InsertLines LineNum, "Me.Pagesetup.LeftFooter = ""Last Change Time: "" & Format(VBA.Time, ""hh:mm:ss"")"
            .InsertLines LineNum + 1, "Me.Pagesetup.RightFooter = ""Last Change Date: "" & Format(VBA.Date, ""dd/mm/yy"")"
        End With
    Next
End Sub

This will add the "Worksheet_Change" event from above to the code section of every sheet.

滥情哥ㄟ 2024-11-13 00:47:47

它修改每个工作表的原因是因为您正在执行“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.

树深时见影 2024-11-13 00:47:47

一种选择可能是每次更改工作表时更新页脚。这意味着各个工作表将具有不同的日期/时间,具体取决于更新时间。

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    With Sh.PageSetup
        .LeftFooter = "Last Save Time: " & Format(VBA.Time, "hh:mm:ss")
        .RightFooter = "Last Save Date: " & Format(VBA.Date, "dd/mm/yy")
    End With
End Sub

然后用户可以正常保存工作簿。

这能满足您的需要吗?

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.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    With Sh.PageSetup
        .LeftFooter = "Last Save Time: " & Format(VBA.Time, "hh:mm:ss")
        .RightFooter = "Last Save Date: " & Format(VBA.Date, "dd/mm/yy")
    End With
End Sub

The user can then save the workbook as normal.

Does this gie you what you need?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文