Results 1 to 7 of 7

Thread: [RESOLVED] 2 Combo Boxes

Hybrid View

  1. #1
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: 2 Combo Boxes

    If I understand, you need a function to return the
    Week Number, given a date. Try this code:

    Code:
    Public Function WeekNumber(InDate As Date) As Long
     Dim DayNo As Long
     Dim StartDays As Long
     Dim StopDays As Long
     Dim StartDay As Long
     Dim StopDay As Long
     Dim VNumber As Long
     Dim ThurFlag As Boolean
    
     DayNo = Days(InDate)
     StartDay = Weekday(DateSerial(Year(InDate), 1, 1)) - 1
     StopDay = Weekday(DateSerial(Year(InDate), 12, 31)) - 1
     ' Number of days belonging to first calendar week
     StartDays = 7 - (StartDay - 1)
     ' Number of days belonging to last calendar week
     StopDays = 7 - (StopDay - 1)
     ' Test to see if the year will have 53 weeks or not
     ThurFlag = (StartDay = 4) Or (StopDay = 4)
     VNumber = (DayNo - StartDays - 4) / 7
     ' If first week has 4 or more days, it will be calendar week 1
     ' If first week has less than 4 days, it will belong to last year's
     ' last calendar week
     If StartDays >= 4 Then
      WeekNumber = Fix(VNumber) + 2
     Else
      WeekNumber = Fix(VNumber) + 1
     End If
     ' Handle years whose last days will belong to coming year's first
     ' calendar week
     If WeekNumber > 52 And ThurFlag = False Then WeekNumber = 1
     ' Handle years whose first days will belong to the last year's
     ' last calendar week
     If WeekNumber = 0 Then
      WeekNumber = WeekNumber(DateSerial(Year(InDate) - 1, 12, 31))
     End If
    
    End Function
    Private Function Days(DayNo As Date) As Long
     Days = DayNo - DateSerial(Year(DayNo), 1, 0)
    End Function
    Function DaysInMonth(YearValue As Long, MonthValue As Long) As Long
     DaysInMonth = Day(DateSerial(YearValue, MonthValue + 1, 0))
    End Function
    'sample useage
    Private Sub Form_Load()
     Dim i As Long
     Dim d As Date
     For i = 1 To 31
      d = DateSerial(2011, 5, i)
      Debug.Print d, WeekNumber(d)
     Next
    End Sub

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    445

    Re: 2 Combo Boxes

    Hi VBClasicRocks

    That is some stunning code thanks, I will now have to work out how to use it.
    It would be a shame not too after I saw the result from the test.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width