Nettoarbeitstage bringen mich zur Verzweiflung
Fettertiger
heute ist wieder so ein Tag an dem mich unser guter Billy wieder mal zum Verzweifeln bringt. Ich versuche die Anzahl der Werktage zwischen Start und Enddatum im Format YYYYMMDD zu errechnen. Die Feiertage befinden sich auf einer separaten Tabelle in einer zum land passenden Spalte. Hier mein "original Macro":
Dim Hol_col as string
Sub datetester()
Dim OStartDate As Date
Dim OEndDate As Date
Dim Duration As Integer
Dim Loc_Hol_Range As Range
Hol_Col = find_hol_col("D")
Set Loc_Hol_Range = tbl_hol.Range(Hol_Col & "3:" & Hol_Col & "9")
tbl_clp.Activate
OStartDate = DateSerial(Left(tbl_clp.Cells(2, 8).Value, 4), Mid(tbl_clp.Cells(2, _
_
8).Value, 5, 2), Right(tbl_clp.Cells(2, 8).Value, 2))
OEndDate = DateSerial(Left(tbl_clp.Cells(2, 10).Value, 4), Mid(tbl_clp.Cells(2, _
_
10).Value, 5, 2), Right(tbl_clp.Cells(2, 10).Value, 2))
If OStartDate OEndDate Then
' tbl_hol.Visible = True
' tbl_hol.Activate
Duration = Application.WorksheetFunction.NetworkDays(OStartDate, OEndDate, _
Loc_Hol_Range)
Else: Duration = 0
End If
End Sub
Function find_hol_col(Country As String) As String
Dim Cntr_col As Integer
'first find column
Cntr_col = 1
Do Until tbl_hol.Cells(1, Cntr_col).Value = Country
If Cntr_col > 50 Then
Exit Do
End If
Cntr_col = Cntr_col + 1
Loop
find_hol_col = Column_Txt_fromNum(Cntr_col)
End Function
Function Column_Txt_fromNum(iNr As Integer)
Column_Txt_fromNum = Left(Cells(1, iNr).Address(0, 0), 1 - (iNr > 26) - (iNr > 702))
End Function
Obwohl alle Werte bis zur Zeile Duration = Application.WorksheetFunction.NetworkDays(OStartDate, OEndDate, Loc_Hol_Range) korrect ermittelt werden, bricht das makro in dieser Zeile immer ab. Ändere ich die Zeile zu Duration = Application.WorksheetFunction.NetworkDays(OStartDate, OEndDate) - also lasse die Feiertagsermittlung weg, läuft das makro weiter und liefert dabei natürlich die falschen Werte falls Feiertage im gesuchten Zeitraum vorkommen.
Was mich dann aber total stutzig machte war das Verhalten, als ich die Beispielmappe für dieses Forum hochladen wollte, denn in der Beispielmappe funzt alles wunderbar. Die Mappe ist identisch mit meiner Problemdatei, beiinhaltet jdedoch ein paar tabellen weniger und hat statt der Tabellee tbl_clp eine neue Tabelle tbl_input. https://www.herber.de/bbs/user/74548.xlsm
Hat jemand eine Idee was ich falsch mache?
Danke und Gruss
Theo