ublic uMode&, StartTime, EndTime
Public MyBook As Workbook, Sht1 As Worksheet, Sht2 As Worksheet, xRow&
Sub 共用參照()
Set MyBook = ThisWorkbook
Set Sht1 = MyBook.Sheets("Sheet1")
StartTime = "08:44:50" '開盤時間(提早十秒開始,才可記錄開盤量價)"08:44:50"
EndTime = "13:45:10" '收盤時間(延後十秒停止記錄)此為測試用時間,請自行更改正式時間 "13:45:10"
End Sub
Sub 自動記錄()
If uMode = 0 Then Exit Sub
If Time > TimeValue(EndTime) Then '收盤時間以後不執行
Sht1.[O8] = "已過收盤"
uMode = 0
Exit Sub
End If
Sht1.Range("B8") = Time '當前時間(時間碼表)
'----------------------------------------------------------
'每1分鐘記錄,5分鐘則改為 Mod 5
If Second(Time) = 0 And Minute(Time) Mod 1 = 0 Then
xRow = Sht1.Range("A65536").End(xlUp).Row + 1
If xRow < 9 Then xRow = 9
Sht1.Range("A" & xRow & ":N" & xRow).Value = Sht1.Range("A8:N8").Value
'------------------------------------------------
If ActiveSheet.Name = Sht1.Name And xRow > 20 Then
ActiveWindow.ScrollRow = xRow - 18 '讓最新資料保持在可見視窗中
End If
ThisWorkbook.Save '存檔
Beep
End If
Application.OnTime Now + TimeValue("00:00:01"), "自動記錄" '每一秒遞迴一次
End Sub
Sub 開始執行()
If uMode = 1 Then Exit Sub
Call 共用參照
uMode = 1
Sht1.[O8] = "執行中"
Call 自動記錄
End Sub
Sub 停止執行()
uMode = 0
Call 共用參照
Sht1.[O8] = "STOP"
End Sub
Sub 清除記錄資料()
Beep
If MsgBox("※確定要清除〔Sheet1〕的記錄嗎? ", 4 + 32 + 256) = vbNo Then Exit Sub
Call 共用參照
Sht1.Rows("9:65536").ClearContents
If ActiveSheet.Name = Sht1.Name Then ActiveWindow.ScrollRow = 1
Beep
End Sub