自动保存上次打开的Excel文件日期

时间:2015-10-16 07:44:45

标签: excel vba

目前我有一个代码,允许我在运行代码时生成当前日期。但是,我想在关闭时自动更新日期,然后当我下次打开它时,我会知道上次打开的日期是什么时候。以下是我目前的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(DashboardActivity.this);
                        builder.setMessage("Look at this dialog!")
                                .setCancelable(false)
                                .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        //do things
                                        dialog.dismiss();
                                    }
                                });
                        AlertDialog alert = builder.create();
                        alert.show();

2 个答案:

答案 0 :(得分:4)

要跟进我的评论:只需将Workbook_BeforeClose的事件处理程序添加到VBA项目的工作簿对象中。

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Home As Worksheet
    Set Home = Worksheets("Program Status Summary")
    Home.Range("A1").Value = Format(Now(), "dd/mmm/yyyy")
End Sub

您可能也希望保存更改,以防止"保存更改"提示。

更新:如果在代码更改之前保存了工作簿,则以下示例会自动保存更改。这是避免保存用户不想要的更改所必需的。

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim wasSaved As Boolean
    wasSaved = ThisWorkbook.saved

    Dim Home As Worksheet
    Set Home = Worksheets("Program Status Summary")
    Home.Range("A1").Value = Format(Now(), "dd/mmm/yyyy")

    If wasSaved Then ThisWorkbook.Save
End Sub

答案 1 :(得分:2)

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Range("A1") = Date
    ThisWorkbook.Save
End Sub

这样做。 " ThisWorkbook.save"部分在添加当前日期后保存工作簿