Ngày tháng trong excel

Liên hệ QC

cadafi

Thành viên gạo cội
Thành viên BQT
Administrator
Tham gia
27/5/07
Bài viết
4,291
Được thích
11,364
Donate (Paypal)
Donate
Giới tính
Nam
Nghề nghiệp
Business Man
Tất cả các vấn đề về ngày tháng trong excel

Trên diễn đàn chúng ta đã có rất nhiều thông tin bổ ích về các hàm ngày tháng trong excel. Mình cũng xin góp vào một ít. Đây là tất cả những gì liên quan đến ngày tháng trong excel, có thể các bạn đã biết, và có thể chưa biết. Hy vọng sẽ có ích cho các bạn.
Nguồn lấy từ : http://www.mvps.org/dmcritchie/excel/datetime.htm
 

File đính kèm

  • DateTime.rar
    79.4 KB · Đọc: 2,322
Lần chỉnh sửa cuối:
Cám ơn bạn Kiệt. Sẳng nói về các hàm ngày tháng trong VBA, hàm dateadd với tham số "w" dếm ngày trong tuần trừ thứ 7 & CN. Thử nhé:

Mã:
 msgbox dateadd("d", 10, now)



Mã:
 msgbox dateadd("w", 10, now)

2 code trên cho cùng 1 kết quả ngày tháng giống nhau. Cái lổ này Microsoft biết nhưng chưa thèm vá. Để khắc phục lổi này thì dùng code sau:

Mã:
'**********************************************************
   'Declarations section of the module
   '**********************************************************
      Option Explicit

   '==========================================================
   ' The DateAddW() function provides a workday substitute
   ' for DateAdd("w", number, date). This function performs
   ' error checking and ignores fractional Interval values.
   '==========================================================
   Function DateAddW(ByVal TheDate, ByVal Interval)

     Dim Weeks As Long, OddDays As Long, Temp As String

     If VarType(TheDate) <> 7 Or VarType(Interval) < 2 Or _
                VarType(Interval) > 5 Then
        DateAddW = TheDate
     ElseIf Interval = 0 Then
        DateAddW = TheDate
     ElseIf Interval > 0 Then
        Interval = Int(Interval)

   ' Make sure TheDate is a workday (round down).
        Temp = Format(TheDate, "ddd")
        If Temp = "Sun" Then
           TheDate = TheDate - 2
        ElseIf Temp = "Sat" Then
           TheDate = TheDate - 1
        End If

   ' Calculate Weeks and OddDays.
        Weeks = Int(Interval / 5)
        OddDays = Interval - (Weeks * 5)
        TheDate = TheDate + (Weeks * 7)

   ' Take OddDays weekend into account.
        If (DatePart("w", TheDate) + OddDays) > 6 Then
           TheDate = TheDate + OddDays + 2
        Else
           TheDate = TheDate + OddDays
        End If

        DateAddW = TheDate
     Else                         ' Interval is < 0
        Interval = Int(-Interval) ' Make positive & subtract later.

   ' Make sure TheDate is a workday (round up).
        Temp = Format(TheDate, "ddd")
        If Temp = "Sun" Then
           TheDate = TheDate + 1
        ElseIf Temp = "Sat" Then
           TheDate = TheDate + 2
        End If

   ' Calculate Weeks and OddDays.
        Weeks = Int(Interval / 5)
        OddDays = Interval - (Weeks * 5)
        TheDate = TheDate - (Weeks * 7)

   ' Take OddDays weekend into account.
        If (DatePart("w", TheDate) - OddDays) < 2 Then
           TheDate = TheDate - OddDays - 2
        Else
           TheDate = TheDate - OddDays
        End If

        DateAddW = TheDate
      End If

   End Function
 
Web KT
Back
Top Bottom