2013-07-13 61 views
0

幫助我使用「C1 FlexGrid」解決Visual Basic編碼問題。如何在Flexgrid中創建標題「每週日期範圍」

如何獲得顯示星期數的日期範圍?

例如:

January 2003 
S M Tu W Th F S 
      1 2 3 4 
5 6 7 8 9 10 11 
12 13 14 15 16 17 18 
19 20 21 22 23 24 25 
26 27 28 29 30 31 

我想顯示

01/01/03 - 04/01/03 as "week 1" of January 03 
01/05/03 - 01/11/03 as "week 2" of January 03 
01/12/03 - 18/01/03 as "week 3" in January 03 ... up "week 5", etc. .. 

有沒有一種方法,我可以做到這一點?

回答

0

未經測試。可能改進。根據您的要求對代碼進行調整。

Public Function DateOfFirstDayofWeek(intCurrentDayofWeek As Integer, WhichDate As Date) As Date 
    DateOfFirstDayofWeek = DateAdd("D", intCurrentDayofWeek * (-1) + 2, WhichDate) 
End Function 

Function dhFirstDayInMonth(dtmDate As Date) As Date 
    dhFirstDayInMonth = DateSerial(year(dtmDate), Month(dtmDate), 1) 
End Function 

Function dhLastDayInMonth(dtmDate As Date) As Date 
    dhLastDayInMonth = DateSerial(year(dtmDate), Month(dtmDate) + 1, 0) 
End Function 

Private Sub getWeekRange_Click() 
Dim startDate As Date 
Dim endDate As Date 
Dim tmpDate As Date 
Dim dateOfMonth As Date 
Dim myDate As Date 

dateOfMonth = CDate("01/02/2013") 
endDate = dhLastDayInMonth(dateOfMonth) 
startDate = dhFirstDayInMonth(dateOfMonth) 
tmpDate = startDate 

While tmpDate < endDate 
    myDate = DateOfFirstDayofWeek(Weekday(tmpDate, vbSunday), tmpDate) ''Assuming Week starts with Sunday 
    tmpDate = DateAdd("d", 6, myDate) 
    If myDate < startDate Then 
     myDate = startDate 
    ElseIf tmpDate > endDate Then 
     tmpDate = endDate 
    End If 
    Debug.Print myDate & "  " & tmpDate 'This Prints the Start date and End date of every week 
Wend 
End Sub 
+0

@Bayusrihernogo,你試過這個,有用嗎? – rags

+0

非常有用謝謝^^ –

+0

@ it.bayu,你需要接受幫助你的答案。通過單擊答案的上/下投票控件下方的刻度標記可以接受答案 – rags