PlanillaHoras_VBA/DateUtils.vba

64 lines
2.0 KiB
Plaintext

' Module: DateUtils
' Description: Functions for date manipulation and validation
Public Function GetDateFromString(value As String) As Date
Dim partesFecha() As String
' Parse date from string in format dd/mm/yyyy
partesFecha = Split(value, "/")
On Error Resume Next
GetDateFromString = DateSerial(CInt(partesFecha(2)), CInt(partesFecha(1)), CInt(partesFecha(0)))
On Error GoTo 0
End Function
Public Function ValidateDateRange(fechaDesde As Date, fechaHasta As Date) As Boolean
' Check if dates are valid
If IsEmpty(fechaDesde) Or IsEmpty(fechaHasta) Then
MsgBox "Por favor, ingrese fechas válidas."
ValidateDateRange = False
Exit Function
End If
' Check that date range doesn't exceed 31 days
If fechaHasta - fechaDesde > 31 Then
MsgBox "El rango de fechas no puede exceder los 31 días."
ValidateDateRange = False
Exit Function
End If
ValidateDateRange = True
End Function
Public Function GetDefaultEndDate() As Date
Dim wsHoras As Worksheet
Dim ultimaFilaConDatos As Long
' Get last date from Horas sheet
Set wsHoras = ThisWorkbook.Sheets(SHEET_HORAS)
ultimaFilaConDatos = wsHoras.Range("F:M").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
GetDefaultEndDate = wsHoras.Cells(ultimaFilaConDatos, 5).Value2
End Function
Public Function GetDefaultStartDate(fechaHasta As Date) As Date
' Default to 28 days before end date
GetDefaultStartDate = DateAdd("d", -28, fechaHasta)
End Function
Public Function SumarPorMes(rangoFechas As Range, mes As Integer, rangoSuma As Range) As Double
Dim sumaTotal As Double
Dim i As Integer
On Error Resume Next
sumaTotal = 0
' Sum values where month matches
For i = 1 To rangoFechas.Count
If Month(rangoFechas.Cells(i).Value2) = mes Then
sumaTotal = sumaTotal + rangoSuma.Cells(i).Value2
End If
Next i
SumarPorMes = sumaTotal
End Function