PlanillaHoras_VBA/Funciones.vba

153 lines
4.9 KiB
Plaintext

Public Sub CargarFDL()
Dim wsHoras As Worksheet
Dim fechaDesde As Date, fechaHasta As Date
Dim ultimaFilaConDatos As Long, ultimaFilaHoras As Long
Dim i As Long, j As Long
Dim nombreHojaHoras As String, nombreHojaFdl_1 As String, nombreHojaNota As String
Dim rangoSeleccionado As Range
' Nombres de las hojas en variables estáticas
nombreHojaHoras = "Horas"
nombreHojaNota = "Nota"
' Establecer referencias a las hojas de trabajo
Set wsHoras = ThisWorkbook.Sheets(nombreHojaHoras)
' Verificar si hay un rango seleccionado en la hoja "Horas" y en la columna E
If Not Application.Selection Is Nothing Then
Set rangoSeleccionado = Application.Selection
If rangoSeleccionado.Worksheet.Name = "Horas" And rangoSeleccionado.EntireColumn.Address = wsHoras.Columns("E").Address Then
' Si es así, usar las fechas en el rango seleccionado
fechaDesde = rangoSeleccionado.Cells(1, 1).Value2
fechaHasta = rangoSeleccionado.Cells(rangoSeleccionado.Rows.Count, 1).Value2
End If
End If
' Si no hay una selección apropiada, utilizar otro método para determinar las fechas
If fechaDesde = 0 Or fechaHasta = 0 Then
' Encontrar la última fila con datos en las columnas F:M en la hoja "Horas"
ultimaFilaConDatos = wsHoras.Range("F:M").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ultimaFilaHoras = wsHoras.Cells(Rows.Count, 5).End(xlUp).Row
' Establecer la fechaDesde y fechaHasta según la última fila con datos en la columna E
fechaHasta = wsHoras.Cells(ultimaFilaConDatos, 5).Value2
fechaDesde = DateAdd("d", -28, fechaHasta)
End If
' Establecer las fechas por defecto en los TextBox
ConsultarFechas.t_hasta.value = Format(fechaHasta, "dd/mm/yyyy")
ConsultarFechas.t_desde.value = Format(fechaDesde, "dd/mm/yyyy")
' Obtener ultimo numero de Factura
ConsultarFechas.frm_factnro.value = ThisWorkbook.Sheets(nombreHojaNota).Cells(6, 3).value
ConsultarFechas.Show
End Sub
' C:/Users/migue/miniconda3/envs/general/python.exe d:/Proyectos/Scripts/InicioApagadoToExcel/LeerLogsToExcel.py
Sub RunLeerLogsToExcel()
Python_ActualizarLogsHorarios
ActualizarDatos
End Sub
Sub AbrirLogsHorarios()
Dim wb As Workbook
' Abre el libro de trabajo especificado
Set wb = Workbooks.Open("D:\Proyectos\Scripts\Horarios\InicioApagadoToExcel\LogEncendidoApagado.xlsx")
' Actualiza todas las conexiones del libro de trabajo abierto
wb.RefreshAll
End Sub
Sub Python_ActualizarLogsHorarios()
Dim objShell As Object
Dim PythonExePath As String
Dim PythonScriptPath As String
' Define la ruta del ejecutable de Python
PythonExePath = "C:\Users\migue\miniconda3\envs\general\python.exe"
' Define la ruta de tu script de Python
PythonScriptPath = "d:\Proyectos\Scripts\Horarios\InicioApagadoToExcel\LeerLogsToExcel.py"
' Define la ruta del archivo donde se guardarán los logs
Dim LogFilePath As String
LogFilePath = "d:\Proyectos\Scripts\Horarios\InicioApagadoToExcel\python_log.txt"
' Crear un objeto Shell
Set objShell = VBA.CreateObject("WScript.Shell")
' Ejecutar el script de Python
objShell.Run "cmd /c " & PythonExePath & " " & PythonScriptPath & " > " & LogFilePath & " 2>&1", 0, True
Set objShell = Nothing
End Sub
Sub ActualizarDatos()
Dim wb As Workbook
' Abre el libro de trabajo especificado
Set wb = Workbooks.Open("D:\Proyectos\Scripts\Horarios\InicioApagadoToExcel\LogEncendidoApagado.xlsx")
' Actualiza todas las conexiones del libro de trabajo abierto
wb.RefreshAll
' Espera un tiempo específico para que se completen las actualizaciones
' Por ejemplo, espera 5 segundos. Ajusta este tiempo según sea necesario
Application.Wait Now + TimeValue("00:00:05")
' Cierra el libro de trabajo sin guardar los cambios
wb.Close SaveChanges:=False
' Actualiza todas las conexiones del libro de trabajo actual (opcional)
ThisWorkbook.RefreshAll
End Sub
Sub SaveComessas()
Menu.b_gen_hoja_comessas_Click
End Sub
Public Sub CargarMENU()
Menu.Show
End Sub
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
' Recorre el rango de fechas y suma los valores si el mes coincide
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
Public Sub Test()
Dim resultado As Double
resultado = SumarPorMes(Sheets("Horas").Range("E3:E367"), 9, Sheets("Horas").Range("O3:O367"))
Debug.Print "La suma para el mes 9 es: " & resultado
End Sub