Cannot run macro when timer is placed.

I'm facing a bit of a bind with my macro in VBA Excel. Previously I have a macro that can extract data from another closed Excel sheet (or one that is opened in a different instance). I placed the code in 'ThisWorkbook' in VBA:

Option Explicit

Private Sub Workbook_Open()
    Call ReadDataFromCloseFile
End Sub

Sub ReadDataFromCloseFile()
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False
   
    Dim src As Workbook
   
    ' OPEN THE SOURCE EXCEL WORKBOOK IN "READ ONLY MODE".
    Set src = Workbooks.Open("C:\Users\mys710048\Documents\EXCEL MONITORING FILE.xlsx", True, True)
   
    ' GET THE TOTAL ROWS FROM THE SOURCE WORKBOOK.
    Dim iTotalRows As Integer
    iTotalRows = src.Worksheets("INFO JPN MOTOR").Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Rows.Count
   
    ' COPY DATA FROM SOURCE (CLOSE WORKGROUP) TO THE DESTINATION WORKBOOK.
    Dim iCnt As Integer         ' COUNTER.
    For iCnt = 1 To iTotalRows
        Worksheets("Sheet1").Range("A1:W1" & iCnt).Formula = src.Worksheets("INFO JPN MOTOR").Range("A1:W1" & iCnt).Formula
    Next iCnt
   
    ' CLOSE THE SOURCE FILE.
    src.Close False             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Set src = Nothing
       
ErrHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
   
End Sub

Now I like to add a timer which will refresh the data every one minute. I placed the code in my Workbook_Open sub:

Private Sub Workbook_Open()
    Call ReadDataFromCloseFile
    Application.OnTime Now + TimeValue("00:01:00"), "ReadDataFromCloseFile"  'Included recently'
End Sub

However, when doing so I receive an error message 1 minute later saying "Cannot run the macro ...The macro may not be available in the workbook or all marcros may be disabled." Which is clearly not the case considering I got it running before I placed the timer. And yes I did save it as xlsm format. Any solutions to this mess?
Hans J.HauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlanConsultantCommented:
What is the 'Included recently'?

Does it work without that in there?

Alan.
0
Hans J.HauAuthor Commented:
Meaning that I added that line in in order to create the timer. Its just a comment. You can ignore it.

What I meant to write was this:

Application.OnTime Now + TimeValue("00:01:00"), "ReadDataFromCloseFile"         'Included recently
0
AlanConsultantCommented:
Of course - sorry.

I have not tried running a procedure that is within the workbook module and I'm not at a PC where I can test right now, but does it make any difference if you move the ReadDataFromCloseFile sub to a standard module?

Alan.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Fabrice LambertFabrice LambertCommented:
I ran some tests.

The procedure you want to trigger must be in a standard module (and of course public).
It doesn't work if it is in a worksheet module or in the workbook module.

Side notes:
- The procedure should re-trigger itself, otherwise it will run only once.
- don't forget to turn off the trigger when you no longer need it.
0
Hans J.HauAuthor Commented:
I tried to move the whole thing to module and change the sub to public. But the timer doesn't want to trigger. I placed a button to manually test and I still get the same message.
0
Fabrice LambertFabrice LambertCommented:
The initial trigger (fonction call) need to be in an event Handler. Like Workbook_open.
0
AlanConsultantCommented:
Hi Hans,

As per my post above, please try moving the ReadDataFromCloseFile sub to a standard module.

Leave everything else as is, and see if that works.

Thanks,

Alan.
0
Hans J.HauAuthor Commented:
I did place it in event handler:

Private Sub Workbook_Open()
    Call ReadDataFromCloseFile
    Application.OnTime Now + TimeValue("00:01:00"), "ReadDataFromCloseFile"
End Sub

This was placed in 'ThisWorkbook' and whenI re-open the file, it only opens a read-only file of data entry every one minute. When I try to manually trigger it I still get the same error.
0
Hans J.HauAuthor Commented:
To Alan,

I tried moving ReadDataFromCloseFile to module. I left Option Explicit and Workbook_Open in ThisWorkbook as normal.

But it only creates a read-only file of data entry file every time I open my display Excel. Even when I manually trigger the macro I still get the error.
0
AlanConsultantCommented:
Okay - You can (and I would suggest you do) have Optional Explicit on every module, but that won't cause a problem either way.

I must be missing something, but when the file is opened, you want it in 'read only mode', so that isn't a problem is it?

Thanks,

Alan.
0
Hans J.HauAuthor Commented:
It is a problem. The main idea of the display is that it can extract data from my data entry file when its closed and copy it into its body. It is supposed to open a read-only file temporarily to get the data and copy it and once its done it should close instantly.

That was the case when I first tested it without the timer in ThisWorkbook. Now that I added this timer it has been messing with my code and I can't figure out where exactly.

Moving ReadDataFromCloseFile sub to module also contributes to the issue, as it causes the read-only file to appear and don't close.
0
Hans J.HauAuthor Commented:
OK. So I did some modification to my code. I did not move anything into module, everything is still in ThisWorkbook.
I changed the code for the timer, placing it inside the ReadDataFromCloseFile sub:

 Sub ReadDataFromCloseFile()
 :
 :
 Application.OnTime Now + TimeValue("00:01.00"), "ThisWorkbook.ReadDataFromCloseFile"
 
Thankfully it works better now. Thanks for all your suggestions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlanConsultantCommented:
Glad to hear its working now :-)

Alan.
0
Hans J.HauAuthor Commented:
After checking various other sources and personally experimenting with the code order, I found out the proper order and method.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.