我不是程序员。我需要excel VBA的帮助。我试图保存每天结束时更改的单元格的值。我想在一天结束时将单元格的值保存在另一个单元格中,然后在单元格接收实时数据后第二天更改为另一个值。我想保存旧数据并将其用于计算。我想每天为我自动做。我如何使用excel做到这一点?非常感谢您的帮助。
答案 0 :(得分:1)
我将假设您希望在每天下午5点或之后保存并关闭您的文件。如果这些假设是正确的,请修改以下代码以适合工作簿的结构:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sht As Worksheet
Dim celLive As Range
Dim celSave As Range
If Hour(Now()) >= 17 Then
' ### change the below values to match your workbook structure ###
Set sht = Sheets("MySheet")
Set celLive = sht.Range("A1")
Set celSave = sht.Range("B1")
celSave.Value = celLive.Value
ActiveWorkbook.Save
End If
End Sub
或者,您可能希望在使用以下代码关闭工作簿之前查看已执行代码的结果:
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sht As Worksheet
Dim celLive As Range
Dim celSave As Range
If Hour(Now()) >= 17 Then
' ### change the below values to match your structure ###
Set sht = Sheets("MySheet")
Set celLive = sht.Range("A1")
Set celSave = sht.Range("B1")
celSave.Value = celLive.Value
With Application
.EnableEvents = False
ActiveWorkbook.Save
.EnableEvents = True
End With
End If
End Sub
将任一方法的代码放在Workbook模块中,否则它将不会执行。