I am using a vba code in Excel for storing a workbook every two minutes. This is running Global, used via network in an Access database. The workbook is showing data from a Counter measures liters on ten different lines.
This VBA code also takes a backup of the spread sheets data at seven o'clock every morning to a sheet in the workbook called History.
When starting the Workbook (Tmb.xls) I am getting the fault "Run-time error '1004': Method 'Sheets' of object '_Global' failed"
In Debug it is stopping in the line: Sheets("Beer Count").Select, its marked in the code.
This means I have to start the macro manually, and then every thing is working OK.
It needs the macro to run automatically on start-up.
Can anybody help? Please?
Option Explicit
'So If I want the macro to run at 07:00:00 and stop at 07:01:50
'and have it run once between those times, as daily backup on an
'other under sheet History.
'I also want to save the open workbook every two minutes.
Public RunWhen As Double
Public Const cRunIntervalSeconds As Long = 120 '60 seconds * 10 minutes
Public Const cRunWhat As String = "SpreadRecordMacro1"
Public Const cRunMinute As String = "Auto_Minute"
Public Const BeginTime As Date = ("07:00:00")
Public Const FinishTime As Date = ("07:01:50")
Public Const StartTime As Date = ("00:00:00")
Sub Auto_Open()
If Time < BeginTime Then
'wait until that time
Application.OnTime earliesttime:=BeginTime, _
procedure:=cRunWhat
Else
'just start it right now????
Application.Run cRunWhat
End If
End Sub
Sub Auto_Close()
'if you're closing the workbook, then
'stop the timer from reopening your workbook
'and running the macro!!
Call StopTimer
End Sub
Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
Schedule:=True
End Sub
Sub SpreadRecordMacro1()
Sheets("Beer count").Select
Range("A4:D20").Select
Selection.Copy
Sheets("History").Select
[a1].Select
ActiveCell.Offset(3, ActiveCell).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
[a1].Select
ActiveCell.Value = ActiveCell.Value + 5
Sheets("Beer count").Select
[a1].Select
ActiveWorkbook.Save
ThisWorkbook.Saved = True
'600/60/24 is 10 minutes
'so check to see if it's too late to schedule the next
'run
If Time > (FinishTime - (cRunIntervalSeconds / 12 / 24)) Then
'don't start it again.
Else
StartTimer
End If
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
Schedule:=False
End Sub
Sub Workbook_Open()
If Time <> StartTime Then
Application.Run cRunMinute
End If
End Sub
Sub Auto_Minute()
Application.OnTime Now + TimeValue("00:02:00"), "Workbook_Open"
'fungerer bare hvis arket er delt
Workbooks("Tmb.xls").AcceptAllChanges Who:="Everyone"
Application.DisplayAlerts = False
Workbooks("Tmb.xls").Save
Application.DisplayAlerts = True
End Sub
Bookmarks