我的 VBA 程式如下:
-------------------------------------------------------------
Option Explicit
Public nRow, Pos As Integer
Private Sub Workbook_Open()
On Error Resume Next
'Application.OnTime TimeValue("08:43:00"), "ThisWorkBook.SyncData"
Call SyncData
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime Now + TimeValue("00:00:01"), "ThisWorkBook.SyncData", , False
End Sub
'*****************************************************************************
' 將 DDE 即時報價資料依時間間隔寫入至報價資料區
'*****************************************************************************
Public Sub SyncData()
On Error Resume Next
'If TimeValue(Now) > TimeValue("13:46:00") Then Exit Sub
With .Sheets("即時資料")
nRow = .Range("A65536").End(xlUp).Row '判斷最後一列有資料的位置
Pos = nRow + 1
Cells(Pos, 1) = Time
Cells(Pos, 3) = Sheets("DDE報價").Cells(7, 3) '台指期成交價
End With
Application.OnTime Now + TimeValue("00:00:30"), "ThisWorkBook.SyncData" '每隔固定設定時間執行寫入至即時資料表
End Sub
-----------------------------------------------------------------------------