Private Sub CommandButton1_Click()
Dim Datum() As String
' Datum splitten
Datum = Split(TextBox1.Text, " - ")
' Datum(0) ist 27.11.07
' Datum(1) ist 06.12.07
' Mit True oder False kannst Du noch festlegen ob der Samstag ein Werktag ist oder nicht.
TextBox2.Text = DateDiffWorkdays(CDate(Datum(0)), CDate(Datum(1)), False)
End Sub
Private Function DateDiffWorkdays(Date1 As Date, Date2 As Date, _
Optional ByVal SaturdayIsWorkday As Boolean) As Integer
Dim nDay1 As Date
Dim nDay2 As Date
Dim nDays As Integer
Dim nWeeks As Integer
Dim nWeekday1 As Integer
Dim nWeekday2 As Integer
If Date1 < Date2 Then
nDay1 = CDate(CLng(CDbl(Date1)))
nDay2 = CDate(CLng(CDbl(Date2)))
Else
nDay1 = CDate(CLng(CDbl(Date2)))
nDay2 = CDate(CLng(CDbl(Date1)))
End If
nDays = nDay2 - nDay1
nWeeks = nDays \ 7
nWeekday1 = Weekday(nDay1, vbMonday)
nWeekday2 = Weekday(nDay2, vbMonday)
If SaturdayIsWorkday Then
nDays = nDays - nWeeks
Select Case nWeekday1
Case 7
Select Case nWeekday2
Case 7
Case Else
nDays = nDays - 1
End Select
Case Else
Select Case nWeekday2
Case 7
Case Else
If nWeekday1 > nWeekday2 Then
nDays = nDays - 1
End If
End Select
End Select
Else
nDays = nDays - 2 * nWeeks
Select Case nWeekday1
Case 6
Select Case nWeekday2
Case 7
nDays = nDays - 1
Case Else
nDays = nDays - 2
End Select
Case 7
Select Case nWeekday2
Case Is < 7
nDays = nDays - 1
End Select
Case Else
Select Case nWeekday2
Case 6
Case 7
nDays = nDays - 1
Case Else
If nWeekday1 > nWeekday2 Then
nDays = nDays - 2
End If
End Select
End Select
End If
DateDiffWorkdays = nDays
End Function