我有一张Excel工作表,可以从另一个已关闭的Excel工作表中获取数据。当我在'VBAProject'中放入'ThisWorkbook'时,我设法让代码工作如下:
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
以前我有一个触发代码的按钮。它不仅可以在我第一次打开文件时自动更新。现在我想在我的代码中添加一个计时器功能,以便它可以经常自动刷新数据。
所以我在Workbook_Open函数中添加了一个计时器,如下所示:
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:01:00"), "ReadDataFromCloseFile"
End Sub
然而,当我让计时器运行时,我收到消息说我无法运行宏,因为它可能在工作簿中不可用或所有宏都被禁用。
显然我的计时器有问题,但我无法弄清楚原因。我甚至把时间都放在了主要的潜水艇上,但仍然没有骰子。有什么建议吗?