how to enter data "OnTime", OR when workbook opened

S

SoupNazi

I need to add "8" to the total amount in a cell automatically on the 1st of
every month. I understand I can use the OnTime function for this if excel is
running at the time that I set. If Excel is not running at that time, can I
set it up to enter that digit the first time I open Excel after the 1st of
the month? I am a beginner at VB, so I really need to be walked through this.
Thanks a lot.
 
B

Bob Phillips

Try this

Private Sub Workbook_Open()
Dim dte As Date
Dim nMonth As Long
On Error Resume Next
dte = Evaluate(ThisWorkbook.Names("date_stored").RefersTo)
On Error GoTo 0
If dte = 0 Or Month(dte) <> Month(Date) Then
Worksheets("Sheet1").Range("A1").Value = _
Worksheets("Sheet1").Range("A1").Value + 8
ThisWorkbook.Names.Add Name:="date_stored", RefersTo:=Date
ThisWorkbook.Names("date_stored").Visible = False
End If
End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
S

SoupNazi

Bob, This seemed to work at first because it immediately added 8 to cell A1
the first time I reopened the workbook. But the cell I needed this in was
AB15 so I changed the two references of A1 to AB15. Nothing gets added to
AB15 now when I open the workbook. Could it be because the code is telling it
to only do this once a month?
 
B

Bob Phillips

Exactly. That it is how I read your request. It won't add it again until you
open the workbook in May.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
S

SoupNazi

Perfect. Thats just what I need I need it to do. Very impressive, Bob. Thanks
a bunch.
 
Top