Beispiel
03.11.2022 11:59:59
Yal
Hallo Shouchen,
kleine Nachtrag. Ich bin sicher, dass Du mit den Beispiele aus dem Netz weit gekommen bist.
Im Grund genommen kann man sich Excel als Beispiel nehmen: die Auslistung (engl.: collection) aller Blätter werden auch per "Name" angesprochen:
Debug.Print Worksheets("Tabelle1").Name
Wenn man es erfasst hat, sind jegliche Spielerei möglich (siehe insb. CollMonat(M.PrevMonth).DayCount ):
Sub test()
Dim CollMonat As Collection
Dim i
Dim M As clsMonat
Set CollMonat = New Collection
For i = 1 To 12
Set M = New clsMonat
M.Name = Format(DateSerial(2022, i, 1), "MMM")
CollMonat.Add M, M.Key
Next
For Each M In CollMonat
Debug.Print M.Name, M.DayCount, M.PrevMonth, CollMonat(M.PrevMonth).DayCount
Next
End Sub
Dabei ist die Klasse clsMonat wie folgt definiert:
Private pName As String
Property Get Key() As String
Key = pName
End Property
Property Let Name(newValue As String)
'SET ONLY ONCE
If pName = "" Then pName = CheckMonth(newValue)
End Property
Property Get Name() As String
Name = pName
End Property
Public Function DayCount() As Long
Dim D As Date
D = CDate("1. " & pName & " " & Year(Now))
DayCount = DateSerial(Year(D), Month(D) + 1, 1) - D
End Function
Public Function PrevMonth() As String
Dim D As Date
D = CDate("1. " & pName & " " & Year(Now))
PrevMonth = Format(DateSerial(Year(D), Month(D) - 1, 1), "MMM")
End Function
Private Function CheckMonth(strMonth As String) As String
On Error Resume Next
CheckMonth = Format(CDate("1. " & strMonth & " " & Year(Now)), "MMM")
End Function
VG
Yal