Cannot run macro when timer is placed.

Hans J.Hau
Hans J.Hau used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlanConsultant

Commented:
What is the 'Included recently'?

Does it work without that in there?

Alan.

Author

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
AlanConsultant

Commented:
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.
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
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.

Author

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.
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
The initial trigger (fonction call) need to be in an event Handler. Like Workbook_open.
AlanConsultant

Commented:
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.

Author

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.

Author

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.
AlanConsultant

Commented:
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.

Author

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.
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.
AlanConsultant

Commented:
Glad to hear its working now :-)

Alan.

Author

Commented:
After checking various other sources and personally experimenting with the code order, I found out the proper order and method.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial