• Hàm tính số năm thâm niên
  • Hàm tính số năm thâm niên

    Xuân Thanh > 04-10-12, 04:52 PM

    Đôi lần có một số bạn hỏi tôi về cách tính thâm niên công tác của CB CNV, yêu cầu chính xác đến từng ngày. Ví dụ ngày vào công tác của Chị Trần Lệ X là ngày 13 tháng 04 năm 1998 thì đến ngày 13 tháng 04 năm 1999 mớ được tính 1 năm. Các bạn có thể dùng hàm IFF kết hợp với DateIf cũng có thể tính ra. Hôm nay giới thiệu vớ các bạn một hàm viết trong VBA để tính. Các bạn tham khảo và vận dụng

    Mã PHP:
    Function ThamNien(StartDate As DateEndDate As Date) As Long
        Dim SoNam 
    As Long
        
    If EndDate StartDate 0 Then MsgBox "Khong tinh duoc tham nien", , "Thong Bao"ThamNien 0: Exit Function
        If 
    Day(StartDate) = Day(EndDate) And Month(StartDate) = Month(EndDateThen
            SoNam 
    Year(EndDate) - Year(StartDate)
        ElseIf 
    Day(StartDate) = Day(EndDate) And Month(StartDate) < Month(EndDateThen
            SoNam 
    Year(EndDate) - Year(StartDate)
        ElseIf 
    Day(StartDate) = Day(EndDate) And Month(StartDate) > Month(EndDateThen
            SoNam 
    Year(EndDate) - Year(StartDate) - 1
        
    ElseIf Day(StartDate) < Day(EndDate) And Month(StartDate) = Month(EndDateThen
            SoNam 
    Year(EndDate) - Year(StartDate)
        ElseIf 
    Day(StartDate) > Day(EndDate) And Month(StartDate) = Month(EndDateThen
            SoNam 
    Year(EndDate) - Year(StartDate) - 1
        End 
    If
        
    ThamNien SoNam
    End 
    Function 

    Thân mến