VBA-带周末的天数减去实际一周的周末数

时间:2018-09-26 18:50:23

标签: excel vba excel-vba excel-formula

我想创建一个VBA代码,您需要花一个工作日,d加上n天数,然后它将返回日期n天数{{ 1}},不包括y一周中的周末。注意:如果d属于周末,则应返回前一个工作日。

y

例如,如果我算是在14天后(包括周末)减去本周的周末,则明天是09/27。可能是这样的:

d + n - (weekend of d week) = y

我如何构建这样的东西?

3 个答案:

答案 0 :(得分:0)

我到了!如果有帮助的人,我会发布代码。

Sub Example()

Dim Day, FinalDay As Date, ActualWeekend As String, DaysToAdd As Integer

DaysToAdd = 14
Day = CDate(Left(Now() + 1, 10)) 'here i add +1 to see if the final day ends on 10/13, but you can put any date as you wish
ActualWeekend = True

For i = 1 To DaysToAdd
    If (Weekday(CDate(Day) + 1) = 7 Or Weekday(CDate(Day) + 1) = 1) And ActualWeekend = True Then
        Day = CDate(WorksheetFunction.WorkDay(Day, 1))
        ActualWeekend = False
    Else
        Day = Day + 1
    End If
Next i

FinalDay = Day

If Weekday(CDate(FinalDay)) = 7 Or Weekday(CDate(FinalDay)) = 1 Then
    FinalDay = CDate(WorksheetFunction.WorkDay(FinalDay, -1))
End If

MsgBox FinalDay

End Sub

答案 1 :(得分:0)

到期日:第一个周末跳过,最后一个周末减少

ifstream myFile;

myFile.open("file.txt");

// Check for errors
if (myFile.fail()) {
    cerr << "Error: File could not be found";
    exit(1);
}

答案 2 :(得分:0)

您也可以使用工作表功能执行此操作。 相当于VBA:

Function dueDate(startDt As Date, numDays As Long) As Date

With Application.WorksheetFunction
    dueDate = .WorkDay(.WorkDay(startDt, 5) + numDays - 5 + 1, -1)
End With

End Function

工作表功能相同:

=WORKDAY(WORKDAY(StartDt,5)+numDays-5+1,-1)
相关问题