Adding a value to a cell when opening the workbook on a new day

sofas

Active Member
Joined
Sep 11, 2022
Messages
469
Office Version
  1. 2019
Platform
  1. Windows
Welcome. Is there a formula or code that can do this, but only when opening the file every morning, because I may open it several times a day?


VBA Code:
Private Sub Workbook_Open()
Sheet1.[A1] = Sheet1.[A1] + 1
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Yes, but you would need a helper cell somewhere. You open the wb and code drops in the date value in the helper cell for that day. Each time you open the wb, the code checks if the date value of that cell is the same as the current date or not and acts accordingly. There may be other more complicated methods that don't involve a helper cell but why go there?
 
Upvote 0
Building on Micron's idea, you could store the last open date in a workbook hidden Name. This is probably a cleaner method.

Use something along these lines :

Code goes in the ThisWorkbook Module:
VBA Code:
Option Explicit

Private Sub Workbook_Open()
    Call IncrementDailyOpenCounter(UpdateCell:=Sheet1.[a1])
End Sub

Private Sub IncrementDailyOpenCounter(ByVal UpdateCell As Range)

    On Error Resume Next
    Debug.Assert [DateStamp]
   
    If Err Then
        Call Me.Names.Add("DateStamp", Date, False)
        GoTo Update
    End If
   
    If Date > [DateStamp] Then
        Me.Names("DateStamp").Value = CLng(Date)
        GoTo Update
    End If
   
    Exit Sub
Update:
    UpdateCell = UpdateCell + 1&
    Me.Save

End Sub
 
Last edited:
Upvote 0
Solution
Building on Micron's idea, you could store the last open date in a workbook hidden Name. This is probably a cleaner method.

Use something along these lines :

Code goes in the ThisWorkbook Module:
VBA Code:
Option Explicit

Private Sub Workbook_Open()
    Call IncrementDailyOpenCounter(UpdateCell:=Sheet1.[a1])
End Sub

Private Sub IncrementDailyOpenCounter(ByVal UpdateCell As Range)

    On Error Resume Next
    Debug.Assert [DateStamp]
  
    If Err Then
        Call Me.Names.Add("DateStamp", Date, False)
        GoTo Update
    End If
  
    If Date > [DateStamp] Then
        Me.Names("DateStamp").Value = CLng(Date)
        GoTo Update
    End If
  
    Exit Sub
Update:
    UpdateCell = UpdateCell + 1&
    Me.Save

End Sub
Thank you, great idea
 
Upvote 0

Forum statistics

Threads
1,215,089
Messages
6,123,058
Members
449,091
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top