G
Gunnar Aronsen, SKM Market Predictor AS
The function below creates week numbers in many different forms.
Public Function WeekValue(Dato As Date, Optional FormatValue As String =
"w", Optional Invert As Boolean = False) As Variant
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Function returing week values in this form.
' "w" = 1,2,...,53
' "ww" = 01,02,..,53
' "w-yy" = 1-yy,2-yy,..,53-yy
' "w-yyyy" = 1-yyyy,2-yyyy,..,53-yyyy
' "ww-yy" = 01-yy,02-yy,..,53-yy
' "ww-yyyy" = 01-yyyy,02-yyyy,..,53-yyyy
' "wyy" = 1-yy,2-yy,..,53-yy
' "wwyy" = 01-yy,02-yy,..,53-yy
' "wwyyyy" = 01-yyyy,02-yyyy,..,53-yyyy
' Invert = Invert order, sets year and then weeks
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Checking input data.
If VarType(Dato) <> vbDate Then
WeekValue = ""
Exit Function
End I
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Use datepart function to find year,week and month number.
YearNo = DatePart("yyyy", Dato, vbMonday, vbFirstFourDays)
WeekNo = DatePart("ww", Dato, vbMonday, vbFirstFourDays)
MonthNo = DatePart("m", Dato, vbMonday, vbFirstFourDays
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Find how many days there is in the current year.
SisteUkeDagIÃ…ret = DatePart("w", CDate("31.12." & YearNo), vbMonday,
vbFirstFourDays)
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Correct year and week number when needed.
If WeekNo = 53 And SisteUkeDagIÃ…ret < 4 Then
WeekNo = 1
YearNo = YearNo + 1
ElseIf WeekNo = 1 And SisteUkeDagIÃ…ret < 4 And MonthNo = 12 Then
YearNo = YearNo + 1
End If
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Setting in hyphen
SpaceValue = ""
If InStr(FormatValue, "-") > 0 Then
SpaceValue = "-"
End If
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Manipulate year and weeknumber if needed.
Select Case FormatValue
Case Is = "w"
YearNo = ""
Case Is = "ww"
WeekNo = IIf(WeekNo < 10, "0" & WeekNo, WeekNo)
YearNo = ""
Case Is = "w-yy", "wyy"
YearNo = Right(CStr(YearNo), 2)
Case Is = "ww-yy", "wwyy"
WeekNo = IIf(WeekNo < 10, "0" & WeekNo, WeekNo)
YearNo = Right(CStr(YearNo), 2)
Case Is = "ww-yyyy", "wwyyyy"
WeekNo = IIf(WeekNo < 10, "0" & WeekNo, WeekNo)
End Select
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Invert order if needed.
If Invert = False Then
WeekValue = WeekNo & SpaceValue & YearNo
ElseIf Invert = True Then
WeekValue = YearNo & SpaceValue & WeekNo
End If
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
End Function
Public Function WeekValue(Dato As Date, Optional FormatValue As String =
"w", Optional Invert As Boolean = False) As Variant
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Function returing week values in this form.
' "w" = 1,2,...,53
' "ww" = 01,02,..,53
' "w-yy" = 1-yy,2-yy,..,53-yy
' "w-yyyy" = 1-yyyy,2-yyyy,..,53-yyyy
' "ww-yy" = 01-yy,02-yy,..,53-yy
' "ww-yyyy" = 01-yyyy,02-yyyy,..,53-yyyy
' "wyy" = 1-yy,2-yy,..,53-yy
' "wwyy" = 01-yy,02-yy,..,53-yy
' "wwyyyy" = 01-yyyy,02-yyyy,..,53-yyyy
' Invert = Invert order, sets year and then weeks
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Checking input data.
If VarType(Dato) <> vbDate Then
WeekValue = ""
Exit Function
End I
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Use datepart function to find year,week and month number.
YearNo = DatePart("yyyy", Dato, vbMonday, vbFirstFourDays)
WeekNo = DatePart("ww", Dato, vbMonday, vbFirstFourDays)
MonthNo = DatePart("m", Dato, vbMonday, vbFirstFourDays
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Find how many days there is in the current year.
SisteUkeDagIÃ…ret = DatePart("w", CDate("31.12." & YearNo), vbMonday,
vbFirstFourDays)
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Correct year and week number when needed.
If WeekNo = 53 And SisteUkeDagIÃ…ret < 4 Then
WeekNo = 1
YearNo = YearNo + 1
ElseIf WeekNo = 1 And SisteUkeDagIÃ…ret < 4 And MonthNo = 12 Then
YearNo = YearNo + 1
End If
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Setting in hyphen
SpaceValue = ""
If InStr(FormatValue, "-") > 0 Then
SpaceValue = "-"
End If
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Manipulate year and weeknumber if needed.
Select Case FormatValue
Case Is = "w"
YearNo = ""
Case Is = "ww"
WeekNo = IIf(WeekNo < 10, "0" & WeekNo, WeekNo)
YearNo = ""
Case Is = "w-yy", "wyy"
YearNo = Right(CStr(YearNo), 2)
Case Is = "ww-yy", "wwyy"
WeekNo = IIf(WeekNo < 10, "0" & WeekNo, WeekNo)
YearNo = Right(CStr(YearNo), 2)
Case Is = "ww-yyyy", "wwyyyy"
WeekNo = IIf(WeekNo < 10, "0" & WeekNo, WeekNo)
End Select
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Invert order if needed.
If Invert = False Then
WeekValue = WeekNo & SpaceValue & YearNo
ElseIf Invert = True Then
WeekValue = YearNo & SpaceValue & WeekNo
End If
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
End Function