We help IT Professionals succeed at work.
Get Started

Cannot run macro when timer is placed.

189 Views
Last Modified: 2018-01-17
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
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE