Guardar hoja especifica de un libro en otro nuevo

Con una macro puedo guardar la hoja activa del libro en otro nuevo, pero el problema esta en que al guardar la hoja en otro libro, la guarda pero un poco más ancha que la original saliéndose de los margenes de impresión.

La macro es la siguiente

Option Explicit
'
Sub EXCELeINFOGuardarHojaComoArchivoNuevo()
'
'Declaramos las variables.
Dim VentanasProtegidas As Boolean
Dim EstructuraProtegida As Boolean
Dim NombreHoja As String
Dim Confirmacion As String
Dim NombreArchivo As String
Dim GuardarComo As Variant
Dim Extension As String
'
'En caso de error.
On Error GoTo ErrorHandler
'
'Validamos si la ventana o la estructura del archivo están protegidos.
VentanasProtegidas = ActiveWorkbook.ProtectWindows
EstructuraProtegida = ActiveWorkbook.ProtectStructure
'
'En caso de estar protegidas mostramos mensaje.
If VentanasProtegidas = True Or EstructuraProtegida = True Then
MsgBox "No se puede ejecutar el comando cuando la estructura del archivo está protegida.", _
vbExclamation, "EXCELeINFO"
Else
'
'Copiamos la hoja y guardamos.
NombreHoja = ActiveSheet.Name
Confirmacion = MsgBox("Desea guardar la hoja '" & NombreHoja & "' como archivo nuevo?", _
vbQuestion + vbYesNo, "EXCELeINFO")
Application.ScreenUpdating = False
If Confirmacion = vbYes Then
ActiveSheet.Select
ActiveSheet.Copy
NombreArchivo = ActiveWorkbook.Name
GuardarComo = Application.GetSaveAsFilename(InitialFileName:=NombreHoja, _
fileFilter:="Libro de Excel(*.xlsx), *.xlsx, Libro de Excel habilitado para macros(*.xlsm), *.xlsm, Libro de Excel 97-2003(*.xls), *.xls,CSV (delimitado por comas)(*.csv),*.csv", _
Title:="EXCELeINFO - guadar hoja activa como archivo nuevo.")
If GuardarComo = False Then
Workbooks(NombreArchivo).Close SaveChanges:=False
Else
With Application.WorksheetFunction
Extension = .Trim(Right(.Substitute(GuardarComo, ".", .Rept(" ", 500)), 500))
End With

Select Case Extension
Case Is = "xlsx"
ActiveWorkbook.SaveAs GuardarComo
Case Is = "xlsm"
ActiveWorkbook.SaveAs GuardarComo, xlOpenXMLWorkbookMacroEnabled
Case Is = "xls"
ActiveWorkbook.SaveAs GuardarComo, xlExcel8
Case Is = "csv"
ActiveWorkbook.SaveAs GuardarComo, xlCSV
Case Else
ActiveWorkbook.SaveAs GuardarComo
End Select
End If
Else
End If
'
End If
'

Exit Sub
'
'En caso de error mostramos un mensaje.
ErrorHandler:
MsgBox "Ha ocurrido un error: " & Err.Description, vbExclamation, "EXCELeINFO"
Workbooks(NombreArchivo).Close SaveChanges:=False
'
End Sub

1 respuesta

Respuesta
1

H o l a:

Tal vez se cambia el formato por cambiar la versión.

Pero prueba lo con lo siguiente, lo que hice fue grabar una macro obteniendo todos los parámetros de impresión de la hoja. Después estoy asignado cada parámetro de la hoja origen en la hoja destino; la prueba la hice cuando se guarda como xlsx, prueba con esa versión y después prueba con las demás.

Option Explicit
'
Sub EXCELeINFOGuardarHojaComoArchivoNuevo()
    '
    'Declaramos las variables.
    Dim VentanasProtegidas As Boolean
    Dim EstructuraProtegida As Boolean
    Dim NombreHoja As String
    Dim Confirmacion As String
    Dim NombreArchivo As String
    Dim GuardarComo As Variant
    Dim Extension As String
    Dim l1, l2, h1, h2
    '
    Set l1 = ThisWorkbook
    Set h1 = l1.ActiveSheet
    'En caso de error.
    On Error GoTo ErrorHandler
    '
    'Validamos si la ventana o la estructura del archivo están protegidos.
    VentanasProtegidas = ActiveWorkbook.ProtectWindows
    EstructuraProtegida = ActiveWorkbook.ProtectStructure
    '
    'En caso de estar protegidas mostramos mensaje.
    If VentanasProtegidas = True Or EstructuraProtegida = True Then
        MsgBox "No se puede ejecutar el comando cuando la estructura del archivo está protegida.", _
        vbExclamation, "EXCELeINFO"
    Else
        '
        'Copiamos la hoja y guardamos.
        NombreHoja = ActiveSheet.Name
        Confirmacion = MsgBox("Desea guardar la hoja '" & NombreHoja & "' como archivo nuevo?", _
        vbQuestion + vbYesNo, "EXCELeINFO")
        Application.ScreenUpdating = False
        If Confirmacion = vbYes Then
            ActiveSheet.Select
            ActiveSheet.Copy
            Set l2 = ActiveWorkbook
            Set h2 = l2.ActiveSheet
            NombreArchivo = ActiveWorkbook.Name
            GuardarComo = Application.GetSaveAsFilename(InitialFileName:=NombreHoja, _
            fileFilter:="Libro de Excel(*.xlsx), *.xlsx, Libro de Excel habilitado para macros(*.xlsm), *.xlsm, Libro de Excel 97-2003(*.xls), *.xls,CSV (delimitado por comas)(*.csv),*.csv", _
            Title:="EXCELeINFO - guadar hoja activa como archivo nuevo.")
            If GuardarComo = False Then
                Workbooks(NombreArchivo).Close SaveChanges:=False
            Else
                With Application.WorksheetFunction
                    Extension = .Trim(Right(.Substitute(GuardarComo, ".", .Rept(" ", 500)), 500))
                End With
                Select Case Extension
                    Case Is = "xlsx"
                        Call ConfigurarPagina(h1, h2)
                        ActiveWorkbook.SaveAs GuardarComo
                    Case Is = "xlsm"
                        ActiveWorkbook.SaveAs GuardarComo, xlOpenXMLWorkbookMacroEnabled
                    Case Is = "xls"
                        ActiveWorkbook.SaveAs GuardarComo, xlExcel8
                    Case Is = "csv"
                        ActiveWorkbook.SaveAs GuardarComo, xlCSV
                    Case Else
                        ActiveWorkbook.SaveAs GuardarComo
                End Select
            End If
        Else
        End If
        '
    End If
    '
    Exit Sub
    '
    'En caso de error mostramos un mensaje.
ErrorHandler:
    MsgBox "Ha ocurrido un error: " & Err.Description, vbExclamation, "EXCELeINFO"
    Workbooks(NombreArchivo).Close SaveChanges:=False
'
End Sub
'
Sub ConfigurarPagina(h1, h2)
'Por.Dante Amor
    With h2.PageSetup
        .PrintTitleRows = h1.PageSetup.PrintTitleRows
        .PrintTitleColumns = h1.PageSetup.PrintTitleColumns
    End With
    h2.PageSetup.PrintArea = h1.PageSetup.PrintArea
    With h2.PageSetup
        .LeftHeader = h1.PageSetup.LeftHeader
        .CenterHeader = h1.PageSetup.CenterHeader
        .RightHeader = h1.PageSetup.RightHeader
        .LeftFooter = h1.PageSetup.LeftFooter
        .CenterFooter = h1.PageSetup.CenterFooter
        .RightFooter = h1.PageSetup.RightFooter
        .LeftMargin = h1.PageSetup.LeftMargin
        .RightMargin = h1.PageSetup.RightMargin
        .TopMargin = h1.PageSetup.TopMargin
        .BottomMargin = h1.PageSetup.BottomMargin
        .HeaderMargin = h1.PageSetup.HeaderMargin
        .FooterMargin = h1.PageSetup.FooterMargin
        .PrintHeadings = h1.PageSetup.PrintHeadings
        .PrintGridlines = h1.PageSetup.PrintGridlines
        .PrintComments = h1.PageSetup.PrintComments
        .PrintQuality = h1.PageSetup.PrintQuality
        .CenterHorizontally = h1.PageSetup.CenterHorizontally
        .CenterVertically = h1.PageSetup.CenterVertically
        .Orientation = h1.PageSetup.Orientation
        .Draft = h1.PageSetup.Draft
        .PaperSize = h1.PageSetup.PaperSize
        .FirstPageNumber = h1.PageSetup.FirstPageNumber
        .Order = h1.PageSetup.Order
        .BlackAndWhite = h1.PageSetup.BlackAndWhite
        .Zoom = h1.PageSetup.Zoom
        .PrintErrors = h1.PageSetup.PrintErrors
        .OddAndEvenPagesHeaderFooter = h1.PageSetup.OddAndEvenPagesHeaderFooter
        .DifferentFirstPageHeaderFooter = h1.PageSetup.DifferentFirstPageHeaderFooter
        .ScaleWithDocHeaderFooter = h1.PageSetup.ScaleWithDocHeaderFooter
        .AlignMarginsHeaderFooter = h1.PageSetup.AlignMarginsHeaderFooter
        .EvenPage.LeftHeader.Text = h1.PageSetup.EvenPage.LeftHeader.Text
        .EvenPage.CenterHeader.Text = h1.PageSetup.EvenPage.CenterHeader.Text
        .EvenPage.RightHeader.Text = h1.PageSetup.EvenPage.RightHeader.Text
        .EvenPage.LeftFooter.Text = h1.PageSetup.EvenPage.LeftFooter.Text
        .EvenPage.CenterFooter.Text = h1.PageSetup.EvenPage.CenterFooter.Text
        .EvenPage.RightFooter.Text = h1.PageSetup.EvenPage.RightFooter.Text
        .FirstPage.LeftHeader.Text = h1.PageSetup.FirstPage.LeftHeader.Text
        .FirstPage.CenterHeader.Text = h1.PageSetup.FirstPage.CenterHeader.Text
        .FirstPage.RightHeader.Text = h1.PageSetup.FirstPage.RightHeader.Text
        .FirstPage.LeftFooter.Text = h1.PageSetup.FirstPage.LeftFooter.Text
        .FirstPage.CenterFooter.Text = h1.PageSetup.FirstPage.CenterFooter.Text
        .FirstPage.RightFooter.Text = h1.PageSetup.FirstPage.RightFooter.Text
    End With
End Sub

':)
S a l u d o s . D a n t e   A m o r
':) Si es lo que necesitas. Recuerda valorar la respuesta. G r a c i a s.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas