Single calculation

Juan C

Board Regular
Joined
Mar 6, 2009
Messages
131
Help me here...
Is there a way for me to have a formula perform its calculation one time only... meaning that if the precedent data changes it (the formula) won't compute again, thus leaving the previous number it calculated unchange...
 
If you pasted in the code into multiple modules or more than once in the same module you could get that error.

I've tested the code and it works fine for me.

The only tweak I would suggest is

Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime dTime, "MyMacro", , False
On Error GoTo 0
End Sub
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Dear VoG

OK... thanks! I got it to work, but remember that yesterday we set it up that each sheet will execute between certain hours only. We added that "if" formula to it for that to happen, using G3 as "1" for not to execute and "0" for it to execute. Now with this last correction, that "if" has been disabled.... Any ideas???

Thanks

If you pasted in the code into multiple modules or more than once in the same module you could get that error.

I've tested the code and it works fine for me.

The only tweak I would suggest is

Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime dTime, "MyMacro", , False
On Error GoTo 0
End Sub
 
Upvote 0
Try

Rich (BB code):
Public dTime As Date
Sub MyMacro()
dTime = Now + TimeValue("00:00:05")
Application.OnTime dTime, "MyMacro"
With Sheets("Sheet1")
    .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = .Range("A1").Value
    .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = .Range("A2").Value
End With
If Now - Date > TimeValue("14:00:00") Then Call StopIt
End Sub

Sub StopIt()
On Error Resume Next
Application.OnTime dTime, "MyMacro", , False
On Error GoTo 0
MsgBox "Timed macro stopped at " & Format(Now, "hh:mm:ss"), vbInformation
End Sub
 
Upvote 0
I have 24 sheets. Each one works within its determined hour. So I think that the time value have to be > 14:00:00 and < 15:00:00

Also can I create as many sub modules as sheets, changing ("Sheet1") to each sheets name?

Thanks

JC

Try

Rich (BB code):
Public dTime As Date
Sub MyMacro()
dTime = Now + TimeValue("00:00:05")
Application.OnTime dTime, "MyMacro"
With Sheets("Sheet1")
    .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = .Range("A1").Value
    .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = .Range("A2").Value
End With
If Now - Date > TimeValue("14:00:00") Then Call StopIt
End Sub
 
Sub StopIt()
On Error Resume Next
Application.OnTime dTime, "MyMacro", , False
On Error GoTo 0
MsgBox "Timed macro stopped at " & Format(Now, "hh:mm:ss"), vbInformation
End Sub
 
Upvote 0
I wouldn't have 24 timers running - that sounds like a recipe for confusion at best.

Try adapting this:

Code:
Public dTime As Date
Sub MyMacro()
dTime = Now + TimeValue("00:00:05")
Application.OnTime dTime, "MyMacro"
With Sheets("Sheet1")
    If Now - Date >= TimeValue("14:00:00") And Now - Date <= TimeValue("15:00:00") Then
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = .Range("A1").Value
        .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = .Range("A2").Value
    End If
End With
With Sheets("Sheet2")
    If Now - Date >= TimeValue("15:00:00") And Now - Date <= TimeValue("16:00:00") Then
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = .Range("A1").Value
        .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = .Range("A2").Value
    End If
End With
'
'put the code for the other 22 sheets here
'
End Sub
 
Upvote 0
OK... great!! This will take me some time to test. I will revert to you once I have tested this formula.

Once again, I have no way to thank you for your collaboration on this matter.

Best regards,

JC

I wouldn't have 24 timers running - that sounds like a recipe for confusion at best.

Try adapting this:

Code:
Public dTime As Date
Sub MyMacro()
dTime = Now + TimeValue("00:00:05")
Application.OnTime dTime, "MyMacro"
With Sheets("Sheet1")
    If Now - Date >= TimeValue("14:00:00") And Now - Date <= TimeValue("15:00:00") Then
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = .Range("A1").Value
        .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = .Range("A2").Value
    End If
End With
With Sheets("Sheet2")
    If Now - Date >= TimeValue("15:00:00") And Now - Date <= TimeValue("16:00:00") Then
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = .Range("A1").Value
        .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = .Range("A2").Value
    End If
End With
'
'put the code for the other 22 sheets here
'
End Sub
 
Upvote 0
Dear VoD:

Should I paste the last instruction you sent to me as a "Insert > Module" or in the first page?

Thanks

JC

I wouldn't have 24 timers running - that sounds like a recipe for confusion at best.

Try adapting this:

Code:
Public dTime As Date
Sub MyMacro()
dTime = Now + TimeValue("00:00:05")
Application.OnTime dTime, "MyMacro"
With Sheets("Sheet1")
    If Now - Date >= TimeValue("14:00:00") And Now - Date <= TimeValue("15:00:00") Then
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = .Range("A1").Value
        .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = .Range("A2").Value
    End If
End With
With Sheets("Sheet2")
    If Now - Date >= TimeValue("15:00:00") And Now - Date <= TimeValue("16:00:00") Then
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Value = .Range("A1").Value
        .Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = .Range("A2").Value
    End If
End With
'
'put the code for the other 22 sheets here
'
End Sub
 
Upvote 0
No, you should replace the existing code with the new code, then expand it by adding the code for the other 22 sheets where indicated.
 
Upvote 0
I have no idea - it works for me. Did you save the workbook then re-open it?
 
Upvote 0

Forum statistics

Threads
1,215,519
Messages
6,125,298
Members
449,218
Latest member
Excel Master

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