Macro running problem

cob98tp

Board Regular
Joined
Nov 18, 2004
Messages
146
Good morning everybody,

I have set up a sheet which collects various different types of data throughout the day. At the end of each day, once everybody has gone home, I want a macro to run which then collates all this data and set up the sheet again for the next day. Mdusoe helped me with the following piece of code which runs 'Mymacro' at a preset time located in cell A1...

Public oldTime As Double

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$1" Then
If oldTime > 0 Then
On Error Resume Next
Application.OnTime oldTime, "MyMacro", , False
On Error Goto 0
End If
oldTime = [A1]
Application.OnTime [A1], "MyMacro"
End If

End Sub

This works fine.....sometimes. Unfortunately it has proved to be unreliable, running some nights and not others. Is this something to do with the way excel refreshes the time and can anything be done to make this more reliable?

Thanks.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Code:
Application.OnTime TimeValue("17:00:00"), "my_Procedure"

has the code run at 5 pm. you can change this to whatever time you want. Change my_Procedure to the macro you want to run.
 
Upvote 0
Thanks for the reply.

It seems a very easy way of solving my problem, but will it definitely work every single night? Even if nobody touches the computer for 2 or 3 days?

The reason I ask is that, as far as I can tell, the original code SHOULD always work as well but doesn't. I'm not sure this is down to the code but the way excel (97) works on my machine.... any clues anybody?

Thanks again.
 
Upvote 0
From what I see of your code is that it runs only when the worksheet changes, so people have to work with the sheet it order to have it "refresh". They also have to work with it after the desired time is elasped (and when they don't, nothing happens).

If you make a function, let's say DoRefresh:

Code:
public function DoRefresh()
  Wait(17:01)
  Application.OnTime TimeValue("17:00:00"), "DoRefresh"
  'code
end function

From what i know, this code should be executed each and every day, without exclusions, as long as the excel file stays open. You can put a check in the workbook_open to look if the code is executed on the given time, and if else execute it, and set a new timer.
 
Upvote 0
Thanks for the continued help..

Unfortunately my VBA skills are sadly lacking and I'm unsure how to set up the code you suggest. Where do I need to insert the DoRefresh code? Also do I just use the first line of code you gave me as a replacement for my code in the first instance.. i.e. still in the Private Sub Worksheet_Change(..) routine in the Sheet code?

Sorry for the novice questions and thanks again for your help!
 
Upvote 0
Ok, think I'm getting there...
Here is what I've put into the sheet module...

Public RunTime As Double
Public WaitTime As Double


Public Function DoRefresh()

RunTime = [A1]
WaitTime = [A1+(1/1440)]

Wait WaitTime
Application.OnTime RunTime, "DoRefresh"

End Function


Private Sub Worksheet_Change(ByVal Target As Range)

RunTime = [A1]
WaitTime = [A1+(1/1440)]

Application.OnTime RunTime, "test"

End Sub

Will this now work everyday without fail, even if nobody makes any changes to the sheet? It works fine when I test it, but obviously haven't been able to test it over a whole day?
 
Upvote 0
this line of code should be put in your Workbook_Open function:

Code:
Application.OnTime TimeValue("17:00:00"), "DoRefresh"

this function should be put in your workbook functions, ie. right under the Workbook_Open function:

Code:
public function DoRefresh()
  call MyMacro
  Wait(TimeValue(TimeValue("17:00:00") + TimeValue("0:01:00")))
  Application.OnTime TimeValue("17:00:00"), "DoRefresh"
end function

if you want to use the value from cell A1 as time value, replace the "17:00:00" with [A1] in the code lines.
 
Upvote 0
Last time (I hope!)

I've put everything in exactly as you suggest but I get the message...


The macro "path\Book1.xls'!DoRefresh'Cannot be found.

what is going wrong?!?

When I try to put this in a seperate module it does not recognise the 'wait' function... Sorry to keep going on about this problem!
 
Upvote 0
if you put it in a module, you can use:

Code:
Application.Wait (..)

instead of 'Wait' (...)

I just figured that out, too :biggrin:
Don't know why the ontime won't run from the workbook, but excel has a lot of strange things.. :p
 
Upvote 0

Forum statistics

Threads
1,202,990
Messages
6,052,948
Members
444,620
Latest member
marialewis16

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