Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Set the Start and End time of Macro

0

Hi Experts,

Hope you are doing well

I am looking for VBA coding to set the Start and end time of said macro for attached excel sheet.

For example: Want to Set the start Macro at 9:00 AM and End at 5:00 PM

Can you please help with the coding solution for the same. 

Would appreciate your response.

Regards,

Revision -1

Hi John,

I have tried the code in the attached file which you have shared earlier, but seems it is not working.

After coping the code to module, it is not giving any result.

Request you Please help to solve the problem.

Would really appreciate your valuable response.

Regards,

Answer
Discuss

Answers

0
Selected Answer

Sunil

It might be enough to modify the macro SetTimer() to include a simple test against the times you say (changes in bold):

Sub SetTimer()
' 178 - TeachExcel.com (modified for start and stop times per day)

If Now < (Date + 0.375) Or Now > (Date + 0.7083333) Then 'test if time is outside working hours
    StopTimer 'stop the timer (if running)

    Else
    Interval = Now + TimeValue("00:00:10")      ' Set your interval here
    Application.OnTime Interval, "MyMacro"      ' name the time & macro to run

End If

End Sub
where 0.375 equates to 9am i.e. the fraction 9/24 (hours). You might want to do something other than StopTimer if the test is proven or the first time it is (but that's up to you).

Hope this helps. 

Response to question Revision 1:

Sunil, in transferring this macro to another file, you need to pay close attention to any named files and sheets (as I said in the discussion below).

It fails to run since MyMacro includes the line:

With Workbooks("Macro Copy paste and Cut paste Testing1.xlsm").Worksheets("Sheet1")
but your filename is slightly different ("Macro Copy paste and Cut paste Testing 1.xlsm". Correct that and it will work okay.

Note however that you have at least one other inconsistency: in sub MyATR you correctly refer to Worksheets("Report") but sub Myclearmacro still refers to Worksheets("Sheet2") which you renamed to Report.

In general you shouldn't expect us to do debugging like this.

Discuss

Discussion

Forgot to say, I assume you now intend to have the workbook running all the time (or at least before and after the shift starts).
John_Ru (rep: 6142) Aug 25, '21 at 11:32 am
Hi John,
I didn't understand your below sentence's, Can I request you to please help me understand below sentence's.
1. You might want to do something other than StopTimer if the test is proven or the first time it is (but that's up to you).
2. Forgot to say, I assume you now intend to have the workbook running all the time (or at least before and safter the shift starts).
Regards,
SunilA (rep: 58) Aug 30, '21 at 3:14 am
Sunil

1. When the timer first stops after 5 pm, you don't have to do anything else but you might need to create a report, reset the cells etc. Your choice.

2. I meant that the workbook needs to be open before 9am and after 5 pm to report properly so you could open/close it manually to suit or just leave it running 24/7 I guess. 
John_Ru (rep: 6142) Aug 30, '21 at 4:38 am
Thank you John for your valuable response.
SunilA (rep: 58) Aug 30, '21 at 6:27 am
Hi John,
I have tried this code in the fresh attached, file but seems it is not working.
Please guide me for the solution.
Regards,
SunilA (rep: 58) Aug 31, '21 at 1:52 am
Sunil

You're not giving me any clues on how it fails but if you only copied that macro, VB Explore should have given you the information to  fix it.

The macro also needs both the StopTimer macro and your timed action macro MyMacro (changed below) so copying all parts below into a module in a new file will work:
Sub SetTimer()
' 178 - TeachExcel.com (modified for start and stop times per day)
 
If Now < (Date + 0.375) Or Now > (Date + 0.7083333) Then 'test if time is outside working hours
    StopTimer 'stop the timer (if running)
 
    Else
    interval = Now + TimeValue("00:00:10")      ' Set your interval here
    Application.OnTime interval, "MyMacro"      ' name the time & macro to run
 
End If
 
End Sub
 
 
Sub StopTimer()
 
Application.OnTime Earliesttime:=interval, Procedure:="MyMacro", Schedule:=False
 
End Sub
 
Sub MyMacro()
MsgBox "Done"
 
End Sub


Also, as we've said before, don;t address questions to people and remember that this isn't a discussion forum, just Q&A.
John_Ru (rep: 6142) Aug 31, '21 at 6:09 am
Hi John,
Noted, your message with thanks.
Would request you to please refer to main question Revision-1 with attached workbook.
Please help me to understand to fix the problem.
Regards,
SunilA (rep: 58) Aug 31, '21 at 7:11 am
Sunil

I am NOT ypur personal Excel fixer- I think you need to try to sort your own problems. 

I will look at your file however but it will be in several hours. 
John_Ru (rep: 6142) Aug 31, '21 at 7:32 am
I am sorry John for any harsh words i used.
SunilA (rep: 58) Aug 31, '21 at 7:38 am
Sunil

See revised Answer (and please take care in such transfers in future).

BTW your file has become a real "Frankenstein's monster", built around a patchwork of various answers from TeachExcel. At some stage, you need to rationalise that before it is used "in anger" (i.e. for production purposes). What's more, you'll need to understand how all the bits work in case it fails in front of your management.
John_Ru (rep: 6142) Aug 31, '21 at 12:40 pm
Thank you John for your valuable response and solution.
SunilA (rep: 58) Aug 31, '21 at 1:43 pm
Okay Sunil but I'm still not sure if you're hearing what I say.
John_Ru (rep: 6142) Aug 31, '21 at 2:46 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login