0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

自動セル

Last updated at Posted at 2023-09-18
Public NextRun As Double

Const SheetName = "main"
Const MinutesCell = "B3"
Const SecondsCell = "C3"
Const ResetCell = "B4"
Const cRunIntervalSeconds = 1 ' 1秒
Const cRunWhat = "UpdateTimer" ' 実行する手続きの名前

Dim OriginalMinutes As Integer, OriginalSeconds As Integer
Dim CurrentMinutes As Integer, CurrentSeconds As Integer
Dim IsPaused As Boolean  ' 一時停止されたかどうかを示すフラグ

' エラーチェックを行う関数
Function IsValidInput(minVal As Variant, secVal As Variant) As Boolean
    If (Not IsNumeric(minVal) Or CInt(minVal) > 59 Or CInt(minVal) < 0) Then
        MsgBox "分は0〜59の数値で入力してください。", vbExclamation
        IsValidInput = False
        Exit Function
    End If
    
    If (Not IsNumeric(secVal) Or CInt(secVal) > 59 Or CInt(secVal) < 0) Then
        MsgBox "秒は0〜59の数値で入力してください。", vbExclamation
        IsValidInput = False
        Exit Function
    End If
    
    If CInt(minVal) = 0 And CInt(secVal) = 0 Then
        MsgBox "有効な時間を入力してください。分と秒はともにゼロにできません。", vbExclamation
        IsValidInput = False
        Exit Function
    End If
    
    IsValidInput = True
End Function

Sub StartTimer()
    Dim minVal, secVal As Variant

    minVal = Worksheets(SheetName).Range(MinutesCell).Value
    secVal = Worksheets(SheetName).Range(SecondsCell).Value
        
    ' エラーチェックを呼び出す
    If Not IsValidInput(minVal, secVal) Then
        Exit Sub
    End If
    
    
    ' 一時停止されていない場合のみ、元の時間を読み込む
    If Not IsPaused Then
        
        OriginalMinutes = CInt(minVal)
        OriginalSeconds = CInt(secVal)
        CurrentMinutes = OriginalMinutes
        CurrentSeconds = OriginalSeconds
    End If

    Call UpdateTimer
    
    ' 一時停止フラグをリセット
    IsPaused = False
End Sub

Sub UpdateTimer()
    If CurrentSeconds = 0 Then
        If CurrentMinutes = 0 Then
            Application.SendKeys ("{DOWN}")
            CurrentMinutes = OriginalMinutes
            CurrentSeconds = OriginalSeconds
        Else
            CurrentMinutes = CurrentMinutes - 1
            CurrentSeconds = 59
        End If
    Else
        CurrentSeconds = CurrentSeconds - 1
    End If
    
    Worksheets(SheetName).Range(MinutesCell).Value = CurrentMinutes
    Worksheets(SheetName).Range(SecondsCell).Value = CurrentSeconds
    
    NextRun = Now + TimeSerial(0, 0, cRunIntervalSeconds)
    Application.OnTime NextRun, cRunWhat
End Sub

Sub StopTimer()
    On Error Resume Next
    Application.OnTime NextRun, cRunWhat, , False
    IsPaused = True
End Sub

Sub ResetTimer()
    Worksheets(SheetName).Range(MinutesCell).Value = ""
    Worksheets(SheetName).Range(SecondsCell).Value = ""
    ' 一時停止フラグをリセット
    IsPaused = False
End Sub

Sub ResetToSelectedCell()
    Range(ResetCell).Select
End Sub

image.png
自動セル移動ツール 備考
間隔(分) 間隔(秒) 自動セル移動させたい間隔を黄色セルに入力
空白の場合は、"00"扱いとなる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?