VBA计算周末天数

时间:2015-10-30 07:16:30

标签: excel vba excel-vba date

我正在寻找方法使用VBA计算两个日期之间周末(周六和周日)的天数

我搜索过网络,但都显示了如何计算工作日(有些人使用DateDiff,有些人使用Networkdays)但是周末没有几天,而且我已经成功地在工作日这样做了。

示例:

从2015年3月10日至2015年10月9日,它应该返回2天(周六和周日,而不是5天(周一,周二,周三,今日,周五)。

<<< 2015年2月11日更新>>>

我尝试根据@ R3uK样式更改代码,但答案是“怪异”,我不明白为什么结果会像那样。这是'代码:

Sub DateWeekDiff()
Sheets("Duplicate Removed").Activate
Dim Date1 As Date, Date2 As Date, StartDate As Date, EndDate As Date
Dim WeekendDays As Long, CountWeekendDays As Long, i As Long
Dim lrow As Long
Dim PRow As Long
Dim CurrentSheet As Worksheet
Set CurrentSheet = Excel.ActiveSheet
FRow = CurrentSheet.UsedRange.Cells(1).Row
lrow = CurrentSheet.UsedRange.Rows(CurrentSheet.UsedRange.Rows.count).Row
WeekendDays = 0

For PRow = lrow To 2 Step -1
'If CurrentSheet.Cells(PRow, "AD").Value <> "" And CurrentSheet.Cells(PRow, "T").Value <> "" Then _
'    CurrentSheet.Cells(PRow, "AP").Value = Abs(DateDiff("d", (CurrentSheet.Cells(PRow, "AD").Value), (CurrentSheet.Cells(PRow, "T").Value)))

For i = 0 To DateDiff("d", CurrentSheet.Cells(PRow, "AD").Value, CurrentSheet.Cells(PRow, "T").Value)
    Select Case Weekday(DateAdd("d", i, CurrentSheet.Cells(PRow, "AD").Value))
        Case 1, 7
            WeekendDays = WeekendDays + 1
    End Select
Next i
    CountWeekendDays = WeekendDays
    CurrentSheet.Cells(PRow, "AL").Value = CountWeekendDays
Next PRow
End Sub

结果转为(例如)AD = 26/1/2015 5:00:00 PM和T = 13/1/2015 8:05:00 AM等于AL = 807878。 循环也非常慢(暂时没有响应)。

3 个答案:

答案 0 :(得分:2)

我的周末日计算功能版本:

Public Function CountWeekendDays(Date1 As Date, Date2 As Date) As Long
    Dim weekDifference As Integer
    Dim weekday1 As Byte
    Dim weekday2 As Byte
    '------------------------------------------------------------------

    weekDifference = VBA.DateDiff("w", Date1, Date2)
    weekday1 = VBA.Weekday(Date1, vbMonday)
    weekday2 = VBA.Weekday(Date2, vbMonday)

    CountWeekendDays2 = VBA.Abs(VBA.DateDiff("w", Date1, Date2) * 2)

    If Date1 < Date2 Then
        CountWeekendDays2 = CountWeekendDays2 + VBA.IIf(weekday1 < 6, 2, 8 - weekday1) + _
                                                VBA.IIf(weekday2 < 6, 0, weekday2 - 5)
        If weekday2 >= weekday1 Then CountWeekendDays2 = CountWeekendDays2 - 2
    Else
        CountWeekendDays2 = CountWeekendDays2 + VBA.IIf(weekday2 < 6, 2, 8 - weekday2) + _
                                                VBA.IIf(weekday1 < 6, 0, weekday1 - 5)
        If weekday1 >= weekday2 Then CountWeekendDays2 = CountWeekendDays2 - 2
    End If

End Function

此函数仅使用算术运算,因此它比使用循环的函数快得多。

答案 1 :(得分:1)

这个功能可以解决问题:

Public Function CountWeekendDays(Date1 As Date, Date2 As Date) As Long 
    Dim StartDate As Date, EndDate As Date, _
        WeekendDays As Long, i As Long 
    If Date1 > Date2 Then
        StartDate = Date2
        EndDate = Date1 
    Else
        StartDate = Date1
        EndDate = Date2 
    End If 
    WeekendDays = 0 
    For i = 0 To DateDiff("d", StartDate, EndDate)
        Select Case Weekday(DateAdd("d", i, StartDate))
            Case 1, 7
                WeekendDays = WeekendDays + 1
        End Select 
    Next i
    CountWeekendDays = WeekendDays 
End Function

由于它是Public Function,在将其放入任何模块后,您可以直接在Excel中使用它=CountWeekendDays(A1,B1)或在您的循环中使用它:

For i = 2 to 50
    variable = CountWeekendDays(Cells(i, "AD"), Cells(i, "T"))
next i

这是你从无用的东西中完成的全部内容:

Sub DateWeekDiff()
    Dim FRow As Long, Lrow As Long, PRow As Long
    Dim CurrentSheet As Worksheet
    Set CurrentSheet = Excel.Sheets("Duplicate Removed")

    With CurrentSheet
        FRow = .UsedRange.Cells(1).Row
        Lrow = .Range("A" & .Rows.Count).End(xlUp).Row

        For PRow = Lrow To 2 Step -1
            .Cells(PRow, "AL").Value = _
                CountWeekendDays(.Cells(PRow, "AD").Value, .Cells(PRow, "T").Value)
        Next PRow
    End With
End Sub

所以你只需要在帖子的开头粘贴函数,然后就像我在上面那样使用它,或者直接在Excel中使用它(这是用于单元格AL2)=CountWeekendDays(AD2,T2)

答案 2 :(得分:1)

Chip Pearson site here

有一个非常好的解决方案

你可以像这样使用它:

days = NetWorkdays2(StartDate, EndDate As Date, 62) '62 is all days except weekends, (2+4+8+16+32)

他还提出了一个可以在没有VBA的情况下直接写入单元格的公式。