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

Timer Vs Open Other Excel

0

Hi All,

I have a timer on an excel file runing perfectly (I was helped on this Forum).

But I also have a button wiht a macro to open other excel file but now is giving me an error 9.

I attached bot excel files.

Don't know what to do. Pressing the button will give that error, also the timer stops and never restart. Before using the timer the button was running just fine but not anymore.

Please can someone help me.

Thank you so much.

Answer
Discuss

Answers

0
Selected Answer

Frex

The first problem is that your new macro in Module 3 must specify the full path and file name with the file extension so should read (change in bold):

Sub Ajuda()

'

' Ajuda Macro



'open the other file...

Workbooks.Open Filename:="C:\Users\SE80211\OneDrive - Repsol\Desktop\error 9\Help File.xlxs"



End Sub

I've done that in the attached file (but tested it on a path on my PC).

Secondly, the tutorial timer was written (I believe) to be a standalone file. It operates on the active workbook. If you launch a new file, that becomes the active workbook (and the macros will look for sheet 2 which does not exist in your Help file).

To get around that, you can make the references to ranges specific to that file. I've done that in the attached file by adding ThisWorkbook. before the references, so for Module 1 the changes (in bold) are:

Option Explicit


Public DownTime As Date


Sub SetTimer()

    ' Just set the time and allow Timer sub do the work

    ThisWorkbook.Worksheets("Sheet2").Range("S2").Value = "00:00:15"

    DownTime = Now + TimeValue("00:00:15")

End Sub


Sub ShutDown()

    Application.DisplayAlerts = False

    ThisWorkbook.Close SaveChanges:=True

End Sub

and in Module 2:

Option Explicit



Dim Interval As Date


Sub Timer()

    ' set timer value (one second)


    Interval = Now + TimeValue("00:00:01")


    If ThisWorkbook.Worksheets("Sheet2").Range("S2").Value <= 0 Then

        ' reached 0 with no running timer (so just need to quit)

        Call ShutDown

        Else

        ' if not 0, decrement S2

        ThisWorkbook.Worksheets("Sheet2").Range("S2") = DownTime - Now

        Application.OnTime EarliestTime:=Interval, Procedure:="Timer"

    End If

End Sub

Now (using the file attached) the timer should keep going (even if you launch and are looking at the Help file) but any activity in the Help file, the timer file or other Excel files may delay the closure of the timer file (as I said before).

Hope this fixes your problem. If so, please remember to mark this Answer as Selected.

Discuss

Discussion

Hi John,
Your dedication is absolutely great, thank you very much.
I am almost there...but now when I press the button I get an error...like the pic below.
Try to re write the path, name and file extension also to other files but keeps this error as well. Can you please tell me what am I doing wrong.

Note the error is "run time error 1004"..impossible to find the second file...
FREX_420 (rep: 6) Jan 31, '23 at 9:46 am
Frex

Look at your discussion point above and you'll see you can't paste an image or upload one.

Please describe your problem more (if possible) but note that I won't be near my PC for 7 hours or more.

I don't think the space in your folder name will cause an issue but try deleting it and correcting your macro to suit.

Note that earlier I was able to launch the timer file then press the Help button and get the Help file opened (from a location on my PC).
John_Ru (rep: 6142) Jan 31, '23 at 9:53 am
Hi John,
Thanks a lot for your time. You made my day.
Its running amazingly.
A tons a thanks.
FREX_420 (rep: 6) Jan 31, '23 at 2:48 pm
Glad to hear you have it working now. Thanks for selecting my Answer, Frex. 
John_Ru (rep: 6142) Jan 31, '23 at 5:24 pm
Add to Discussion


Answer the Question

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