2017-06-07 4 views
1

私は、現在の日付に対して、列Aにある最新の日付セルをチェックしようとしています。差異が30日であれば、私は新しい行を書きます。私の簡単なExcel VBAマクロをデバッグするには?

私が実行すると、私はシート上のCheckAttendanceを呼び出すことができないと言います(Occulnces)。しかし、なぜ?

Option Explicit 

    Public LastCell As Long 
    Public today As Date 


    Function CheckAttendance() 

     Dim DaysSinceOcc As Integer 

     'returns last occupied row 
     LastCell = Cells(Rows.Count, 1).End(xlUp).Row 

     'gets current date 
     today = Date 

     'subtracts last cell in specified column from today's date. 
     DaysSinceOcc = today - Cells(LastCell, 1).Value 

     'writes what I want written in the cells I want it written in.   
     If DaysSinceOcc > 29 Then 
      Cells(LastCell, 1).Offset(1, 1) = "winback" 
      Cells(LastCell, 1).Offset(1, 2) = -0.5 
      Cells(LastCell, 1).Offset(1, 4) = "Earned back 0.5 pts for 30 days perfect attendance (AutoGenerated)" 
      Cells(LastCell, 1).Offset(1, 5) = "AUTO" 
      Cells(LastCell, 1).Offset(1, 0) = today 
     Else 

     End If 


    End Function 

    Sub Attendance() 

     Sheets("Occurences").CheckAttendance 
     'yes Occurences is suppose to be spelled like that (don't ask) 

    End Sub 

編集:おそらくこれには複数の問題があります。私は多くのことを修正しましたが、つづりました。

+0

'シート( "出現箇所")なしでちょうど' CheckAttendance'を使用しています。 ' – Slai

+0

@ Slaiのコメントに加えて、1)それは機能ではなくサブでなければならない。 2)コード内のセルへの適切な参照を提供する必要があります(ワークブックとワークシートを明示的に参照してください。そうでないと、コードが不安定になる可能性があります)。 – RADO

+0

@RADOご意見ありがとうございます。あなたのコメントはA.S.Hと比較してわかりました。答えは私は2つの異なる選択肢があります:ワークシートを異なるワークシートで呼び出すことを可能にするパラメータをワークシートにするか、より具体的にすることができるので、Occurencesシートでのみ機能します。私はそれを正しく理解しているのですか、そうではないと私は思っています。 –

答えて

2

CheckAttendanceルーチンをパラメータ化して、異なるワークシートで呼び出すことができるようです。これを行うには、ワークシートをパラメータとして受け取るSubにします。また

  • あなたの細胞を修飾し、コードの範囲である
  • はあなたの変数を暗くし、Option Explicit

Option Explicit 

Sub CheckAttendance(ws As Worksheet) 
    Dim DaysSinceOcc As Long, lastRow As Long, today As Long 
    lastRow = ws.Cells(ws.Rows.count, 1).End(xlUp).row 

    'gets current date 
    today = Date 

    'subtracts last cell in specified column from today's date. 
    DaysSinceOcc = today - ws.Cells(lastRow, 1).Value2 

    'writes what I want written in the cells I want it written in. 
    If DaysSinceOcc > 29 Then 
     ws.Cells(lastRow + 1, 1) = today 
     ws.Cells(lastRow + 1, 2) = "winback" 
     ws.Cells(lastRow + 1, 3) = -0.5 
     ws.Cells(lastRow + 1, 5) = "Earned back 0.5 pts for 30 days perfect attendance (AutoGenerated)" 
     ws.Cells(lastRow + 1, 6) = "AUTO" 
    End If 
End Sub 

Sub Attendance() 
    CheckAttendance Sheets("Occurences") ' <-- this is how you call it on any worksheet 
End Sub 
+0

ありがとう。 Variables - Dim vs. Public: 同じ本の他のマクロで変数を使用できるようにするには、パブリックを使用する必要があるという印象を受けました。私の考えでは、VBA-Public = python-globalです。あれは正しいですか? すべての変数をローカルにする理由は何ですか? –

+0

@ChristopherGraves私たちはこのスニペットにはpublic変数を持たず、ローカルのみです。パブリック/グローバル変数はルーチン*外で宣言され、多くのルーチンからアクセスできるようにします。 *データ隠蔽*(カプセル化とも呼ばれます)の原則によって、それらは本当に必要でない限り避けるべきです。どうして?コードを追跡して維持することをより困難にする傾向があるためです。 –

+0

ありがとうございました! –

関連する問題