私は VBA を初めて使用し、作成しようとしているマクロで If ステートメントを使用するのに苦労しています。毎月、当社のどの従業員が特定のタスクを実行したかを示す Excel のレポートを受け取ります。私が書いているマクロは、各従業員のデータをマスター ブックの名前の下にコピー アンド ペーストするためのものです。
私が直面している問題は、コピーする必要がある範囲を定義することです。コードでわかるように、従業員は列 B にリストされています。まず、列 B で従業員を検索します。従業員が存在しない場合、マクロはマスター ブックの従業員の名前の下に (なし) をコピーして貼り付けます。 . 名前が見つかった場合は、その名前の下の行を最初の変数として設定します。
ここで私は問題に遭遇します。次のステップは、リストされている次の従業員を見つけ、上の行を 2 番目の変数として設定することです。次に、1 番目と 2 番目の変数を使用して、その範囲の行をコピーして貼り付けます。If ステートメントを使用して循環し、リストされている次の従業員を見つけています。ただし、ネストされた If ステートメントは、2 番目の Else if ステートメントの後に終了しています。これをもっとうまく書く方法を知っている人はいますか?Select Case ステートメントを使用してみましたが、正しい構文を取得できませんでした。
Sub EmployeeActivity()
Dim Employee1 As Integer, Employee2 As Integer, Employee3 As Integer, Employee4 As Integer
Dim EmployeeRange As Range, rngSelectFind As Range, rngPasteFind As Range
Windows("Activities Report.xlsm").Activate
Set rngSelectFind = Columns("B:B").Find(What:="Employee 1", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee1 = rngSelectFind.Row + 1
ElseIf rngSelectFind Is Nothing Then
Set rngSelectFind = Columns("B:B").Find(What:="(none)", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
Consultant3 = rngSelectFind.Row
End If
Set rngSelectFind = Columns("B:B").Find(What:="Employee 2", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee2 = rngSelectFind.Row - 1
ElseIf rngSelectFind Is Nothing Then
Set rngSelectFind = Columns("B:B").Find(What:="Employee 3", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee2 = rngSelectFind.Row - 1
End If
ElseIf rngSelectFind Is Nothing Then
Set rngSelectFind = Columns("B:B").Find(What:="(none)", After:=Range("B1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngSelectFind Is Nothing Then
Employee2 = rngSelectFind.Row - 1
End If
End If
If Employee1 > 0 And Employee2 > 0 Then
Set EmployeeRange = Range(Cells(Employee1, 2), Cells(Employee2, 7))
ElseIf Employee3 > 0 Then
Set EmployeeRange = Range(Cells(Employee3, 2), Cells(Employee3, 7))
End If
EmployeeRange.Select
Selection.Copy
Windows("Monthly Activity Report.xlsm").Activate
Sheets("April '13").Activate
Set rngPasteFind = Columns("A:A").Find(What:="Employee Activities", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not rngPasteFind Is Nothing Then
Employee4 = rngPasteFind.Row + 1
End If
Range(Cells(Employee4, 1), Cells(Employee4, 6)).Select
Selection.Insert (xlShiftDown)
End Sub
よろしくお願いします。追加のコンテキストを提供できるかどうか教えてください。