• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 108
  • Last Modified:

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?
0
Hans J.Hau
Asked:
Hans J.Hau
  • 7
  • 5
  • 2
1 Solution
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now