根据开始日期和结束日期的差异自动将多个条目填充到日历

时间:2015-02-26 17:35:13

标签: excel

我尝试编写VBA以在摘要表中查找日期,并将数据填充到日历中以进行员工休假跟踪。

“摘要”页面中的数据如下所示

月员工假期类型开始日期结束日期时间 2月卡尔半日下午2/26/2015 2/26/2015
2月Hurness半日下午2/26/2015 2/26/2015
2月Edna半日AM 1/18/2016 2/26/2015

我编写了下面的代码来填充单行。我想知道如何根据开始和结束日期的差异将多个条目填充到日历中

提前感谢您的帮助!

Sub AddToCalendar()

Dim R As Range
Dim lastRow As Long
Dim startDate As Integer
Dim Employee As String
Dim Reason As String
Dim Time As String
Dim sSheet As String

'locate the info in the last row of the Summary sheet
lastRow = Sheets("Summary").Cells(Rows.Count, 4).End(xlUp).row
Employee = Sheets("Summary").Cells(lastRow, 2).Value
Reason = Sheets("Summary").Cells(lastRow, 3).Value
Time = Sheets("Summary").Cells(lastRow, 6).Value


'active the worksheet of relevant month
sSheet = Sheets("Summary").Cells(lastRow, 1).Value
Worksheets(sSheet).Activate

'locate the cell of specific date and enter data
startDate = Day(Sheets("Summary").Cells(lastRow, 4).Value) 
endDate = Day(Sheets("Summary").Cells(lastRow, 5).Value)

With Sheets(sSheet)
    If startDate = endDate Then
        Set R = .Range("A1:H58").Find(startDate)
         If Not R Is Nothing Then
             Sheets(sSheet).Cells(R.row + 1, R.Column).Value = Employee & " " & Reason & " " & Time
         End If

     Else
         Do Until startDate = endDate
             startDate = startDate + 1
             Set R = .Range("A1:H58").Find(startDate)
             If Not R Is Nothing Then
                 Sheets(sSheet).Cells(R.row + 1, R.Column).Value = Employee & " " & Reason & " " & Time
             End If
         Loop

     End If

 End With

End Sub

2 个答案:

答案 0 :(得分:1)

我试图添加代码以跳过周末,但我对这里的逻辑感到有点困惑。这就是我所做的,你能看一眼,看看有什么不对吗?非常感谢!

  For i = 1 To TotalDaysOff
            With Sheets(sSheet)
                Set R = .Range("A1:H58").Find(startDate + (i - 2))
                If Not R Is Nothing Then
                    Sheets(sSheet).Cells(R.row + 1, R.Column).Value = Employee & " " & Reason & " " & Time

                    If skipWeekend >= 6 Then
                        Sheets(sSheet).Cells(R.row + 1, R.Column).Value = ""
                    Else
                        Sheets(sSheet).Cells(R.row + 1, R.Column).Value = Employee & " " & Reason & " " & Time
                    End If

                End If

答案 1 :(得分:0)

要根据日期范围(不同的开始日期和结束日期)在摘要表上输入多行,最好的办法是首先确定员工休假的天数。这是一个相当简单的算术计算,例如:

TotalDaysOff = EndDate - StartDate + 1

[注意:我们必须在公式中加1才能获得正确的天数。例如2/26/2015 - 2 / 26-2015将等于0,但我们知道它实际上是1]。

一旦我们计算了TotalDaysOff,我们就可以创建一个简单的循环来填充每一行,例如:

If TotalDaysOff = 1 then
    With Sheets(sSheet)
        Set R = .Range("A1:H58").Find(startDate)
        If Not R Is Nothing Then
            Sheets(sSheet).Cells(R.row + 1, R.Column).Value = Employee     & " " & Reason & " " & Time
        End If

    End With
Else
    for i = 1 to TotalDaysOff
        With Sheets(sSheet)
            Set R = .Range("A1:H58").Find(startDate + (i - 1))
            If Not R Is Nothing Then
                Sheets(sSheet).Cells(R.row + 1, R.Column).Value = Employee & " " & Reason & " " & Time
            End If

        End With

    Next i
End If

这对你有用吗?