Avatar of Hans J.Hau
Hans J.Hau
 asked on

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?
Microsoft OfficeVBA* timerMicrosoft Excel

Avatar of undefined
Last Comment
Hans J.Hau

8/22/2022 - Mon
Alan

What is the 'Included recently'?

Does it work without that in there?

Alan.
Hans J.Hau

ASKER
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
Alan

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Fabrice Lambert

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.
Hans J.Hau

ASKER
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 Lambert

The initial trigger (fonction call) need to be in an event Handler. Like Workbook_open.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Alan

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.
Hans J.Hau

ASKER
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.
Hans J.Hau

ASKER
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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Alan

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.
Hans J.Hau

ASKER
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.
ASKER CERTIFIED SOLUTION
Hans J.Hau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Alan

Glad to hear its working now :-)

Alan.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Hans J.Hau

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