0% found this document useful (0 votes)
7 views2 pages

Codigo Final GenerarImpresion

Uploaded by

caredguerra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Codigo Final GenerarImpresion

Uploaded by

caredguerra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sub GenerarImpresion()

Dim Data As Worksheet


Dim Impresion As Worksheet
Dim CopyRng As Range, PasteRng As Range
Dim lastRow As Long
Dim saveLocation As String

' Definir las hojas


Set Data = ThisWorkbook.Sheets("Nomina")
Set Impresion = ThisWorkbook.Sheets("Impresion")

' Desproteger hojas necesarias


Data.Unprotect "arabito2023*"
Impresion.Unprotect "arabito2023*"

' Obtener última fila de datos


lastRow = Data.Range("A:A").Find(What:="*", SearchOrder:=xlRows,
SearchDirection:=xlPrevious, LookIn:=xlValues).Row

' Limpiar rango previo en la hoja de impresión


Impresion.Range("A4:R" & Impresion.Rows.Count).ClearContents

' Copiar datos principales de la hoja "Nomina"


Set CopyRng = Data.Range("A8:C" & lastRow)
Set PasteRng = Impresion.Range("A4")
CopyRng.Copy
PasteRng.PasteSpecial xlPasteValuesAndNumberFormats
PasteRng.PasteSpecial xlPasteFormats
Application.CutCopyMode = False

' Copiar otros datos relevantes


Set CopyRng = Data.Range("AC8:AP" & lastRow)
Set PasteRng = Impresion.Range("I4")
CopyRng.Copy
PasteRng.PasteSpecial xlPasteValuesAndNumberFormats
PasteRng.PasteSpecial xlPasteFormats
Application.CutCopyMode = False

' Ajustar columna de asistencia


Set CopyRng = Data.Range("T7:X" & lastRow)
Set PasteRng = Impresion.Range("D4")
CopyRng.Copy
PasteRng.PasteSpecial xlPasteValuesAndNumberFormats
PasteRng.PasteSpecial xlPasteFormats
Application.CutCopyMode = False

' Calcular suma total en la columna de valores (columna "V")


lastRow = Impresion.Cells(Impresion.Rows.Count, "A").End(xlUp).Row
Impresion.Cells(1, 22).Formula = "=SUM(V4:V" & lastRow & ")"

' Ajustar formato de columnas para mejor visualización


Impresion.Range("C:C").ShrinkToFit = True

' Activar hoja de impresión y guardar como PDF en la misma ubicación del
archivo principal
Impresion.Activate
saveLocation = ThisWorkbook.Path & "\Nomina_Impresion_" & Format(Now, "yyyy-mm-
dd_HH-MM-SS") & ".pdf"
Impresion.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation,
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,
OpenAfterPublish:=False

' NO LIMPIAR el rango después de exportar para que los datos permanezcan
visibles

' Proteger nuevamente las hojas


With Data
.Protect Password:="arabito2023*", AllowFiltering:=True, AllowSorting:=True
.EnableSelection = xlNoRestrictions
End With
With Impresion
.Protect Password:="arabito2023*", AllowFiltering:=True, AllowSorting:=True
.EnableSelection = xlNoRestrictions
End With

End Sub

You might also like