LoginSignup
0
0

More than 3 years have passed since last update.

【VBA】勤怠管理データのチェック

Posted at

とある企業の勤怠管理情報のチェックを行うプログラム。
問題のある部分に色を付けるだけのプログラムだが基本的な要素が詰まっているのでシェア。
ポイントはコメント行による。

勤怠管理データのチェック
Sub kinmuCheck()
    'Debug.Printは必修のコマンド。イミディエイトウインドウ(VBE内の機能)に現在の変数の値などを表示することができる。 
    Debug.Print "=== 処理開始==="

    Dim i As Integer
    i = 1
    Dim n As Integer 'データの行数

    Do While Cells(i, 1) <> ""  'Do While〜Loop構文
        i = i + 1
    Loop
    n = i - 1

     Debug.Print "読込データの行数:" & n

    ' チェックカラーの設定 =>checkColorの変数値を変更する
    Dim checkColor As String
    checkColor = vbCyan  'vbRed, vbBlue, vbGreen, vbMagenta, vbCyan, vbYellow
    '色変数(vbYellow)は文字列(String)扱い。でもダブルコーテーション(”)は不要。

    For i = 2 To n
    'Callステートメントの例。メインルーチンから渡す値が括弧内に入っている。
        Call sigyoCheck(i, checkColor)

        Call nyukanCheck(i, checkColor)

        Call logOnCheck(i, checkColor)

        Call logOffCheck(i, checkColor)

        Call taikanCheck(i, checkColor)

        Debug.Print i & " 行目の処理完了"

    Next i

End Sub

'サブルーチンの例。引数として欲しい値を括弧内に記述。エラーを避けるため変数型を指定しておくとよい。
Sub sigyoCheck(i As Integer, checkColor As String)

        ' 空欄と文字(スペース記号含む)のスキップ
        If Cells(i, 16).Value = "" Then
            GoTo Continue   'ループ処理を抜けるための処理。後の行にあるContinue:とセットになっている。
        ElseIf TypeName(Cells(i, 16).Value) = "String" Then
       'TypeName()は値の型を確認するコマンド。
            GoTo Continue
        End If

        '   始業時刻の取得
        Dim shigyo As Date
        shigyo = Cells(i, 16).Value

        '  分の取得
        Dim mm As Integer
        mm = Minute(shigyo)
        'Minute()はDate型の数値から「分」の値を抜き出すコマンド

        If mm Mod 10 <> 0 Then
            Cells(i, 16).Interior.Color = checkColor
        End If

Continue:

End Sub

Sub nyukanCheck(i, checkColor)

        ' 空欄と文字(スペース記号含む)のスキップ
        If Cells(i, 13).Value = "" Then
            GoTo Continue
        ElseIf TypeName(Cells(i, 13).Value) = "String" Then
            GoTo Continue
        End If

        '  入館時刻の取得
        Dim nyukan As Date
        nyukan = Cells(i, 13).Value
        '   始業時刻の取得
        Dim shigyo As Date
        shigyo = Cells(i, 16).Value
         ' 差を計算( 分単位 )
        Dim gap As Date
        gap = DateDiff("n", nyukan, shigyo)
        'Datediff()は2つの時刻の差を算出するもの。1番目のプロパティで単位を決定する。

        If gap < 0 Then
            Cells(i, 19).Interior.Color = checkColor
        ElseIf gap > 60 Then
            Cells(i, 19).Interior.Color = checkColor
        End If

Continue:

End Sub


Sub logOnCheck(i, checkColor)

        ' 空欄と文字(スペース記号含む)のスキップ
        If Cells(i, 15).Value = "" Then
            GoTo Continue
        ElseIf TypeName(Cells(i, 15).Value) = "String" Then
            GoTo Continue
        End If

        '   ログオン時刻の取得
        Dim logOn As Date
        logOn = Cells(i, 15).Value
        '   始業時刻の取得
        Dim shigyo As Date
        shigyo = Cells(i, 16).Value
         ' 差を計算( 分単位 )
        Dim gap As Date
        gap = DateDiff("n", logOn, shigyo)

        If gap < 0 Then
            Cells(i, 20).Interior.Color = checkColor
        ElseIf gap > 30 Then
            Cells(i, 20).Interior.Color = checkColor
        End If

Continue:

End Sub


Sub logOffCheck(i, checkColor)

        ' 空欄と文字(スペース記号含む)のスキップ
        If Cells(i, 31).Value = "" Then
            GoTo Continue
        ElseIf TypeName(Cells(i, 31).Value) = "String" Then
            GoTo Continue
        End If

        '   ログオフ時刻の取得
        Dim logOff As Date
        logOff = Cells(i, 31).Value
        '    終業時刻の取得
        Dim syugyo As Date
        syugyo = Cells(i, 32).Value
         ' 差を計算( 分単位 )
        Dim gap As Date
        gap = DateDiff("n", logOff, syugyo)

        If gap < 0 Then
            Cells(i, 36).Interior.Color = checkColor
        ElseIf gap > 9 Then
            Cells(i, 36).Interior.Color = checkColor
        End If

Continue:

End Sub

Sub taikanCheck(i, checkColor)

        ' 空欄と文字(スペース記号含む)のスキップ
        If Cells(i, 29).Value = "" Then
            GoTo Continue
        ElseIf TypeName(Cells(i, 29).Value) = "String" Then
            GoTo Continue
        End If

        '   ログオフ時刻の取得
        Dim logOff As Date
        logOff = Cells(i, 31).Value
        '    終業時刻の取得
        Dim syugyo As Date
        syugyo = Cells(i, 32).Value
        ' 退館時刻の取得
        Dim taikan As Date
        taikan = Cells(i, 29).Value

        Dim gap As Date

         '終業と退館の乖離
        gap = DateDiff("n", syugyo, taikan)

        If gap < 0 Then
            Cells(i, 35).Interior.Color = checkColor
        ElseIf gap > 30 Then
            Cells(i, 35).Interior.Color = checkColor
        End If

        ' ログオフと退館の乖離
        gap = DateDiff("n", logOff, taikan)

        If gap < 0 Then
            Cells(i, 34).Interior.Color = checkColor
        ElseIf gap > 30 Then
            Cells(i, 34).Interior.Color = checkColor
        End If

Continue:

End Sub
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