I have an Excel sheet that gets data from another closed Excel sheet. I managed to get the code working as follows when placed in 'ThisWorkbook' in my 'VBAProject':
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("Source file path", True, True)
' GET THE TOTAL ROWS FROM THE SOURCE WORKBOOK.
Dim iTotalRows As Integer
iTotalRows = src.Worksheets("data entry").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("display").Range("A1:W1" & iCnt).Formula = src.Worksheets("data entry").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
Previously I have a button to trigger the code. Not only that it can auto update when I first open the file. Now I would like to add a timer function to my code so that it can refresh the data frequently and automatically.
So I added a timer to my Workbook_Open function as follows:
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:01:00"), "ReadDataFromCloseFile"
End Sub
However when I let the timer run I receive the message saying I cannot run the macro because it may not be available in workbook or all macros is disabled.
Clearly my timer has issues, but I can't figure out why. I even placed the time into the main sub but still no dice. Any suggestions?