Como puedo hacer una macro para imprimir las hojas que desee

Quiero hacer una macro con la cual pueda imprimir las hojas de mi libro hasta una celda que cumpla la condiccion de que esta el numero de cuenta bancaria y que me aparezca un userform donde yo pueda seleccionar las hojas que quiero imprimir

2 respuestas

Respuesta
2

Va el código

Dim hojas
Dim cargando
'
Private Sub CommandButton1_Click()
'Por.Dante Amor
    Dim Pdfhojas()
    Dim HojasOcultas()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    '
    hojaactiva = ActiveSheet.Name
    n = -1
    m = -1
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) Then
            h = ListBox1.List(i)
            n = n + 1
            ReDim Preserve Pdfhojas(n)
            Pdfhojas(n) = h
            wvis = Sheets(h).Visible
            If wvis <> -1 Then
                m = m + 1
                ReDim Preserve HojasOcultas(m)
                HojasOcultas(m) = h
                Sheets(h).Visible = -1
            End If
        End If
    Next
    If n > -1 Then
        ruta = ThisWorkbook.Path & "\"
        arch = "varias hojas"
        '
        'Guarda archivo PDF
        If CheckBox1.Value = True Then
            Sheets(Pdfhojas).Select
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                Filename:=ruta & arch & ".pdf", _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False
        End If
        '
        'Imprime las hojas
        If CheckBox2.Value = True Then
            Sheets(Pdfhojas).PrintOut
        End If
        '
        'Guarda archivo como xlsx
        If CheckBox3.Value = True Then
            Sheets(hojas).Copy
            ActiveWorkbook.SaveAs _
                Filename:=ruta & arch & ".xlsx", _
                FileFormat:=xlExcel12, CreateBackup:=False
            ActiveWorkbook.Close False
        End If
        '
        'Guarda archivo como Binario
        'Sheets(hojas).Copy
        'ActiveWorkbook.SaveAs _
            Filename:=ruta & arch, _
            FileFormat:=xlExcel12, CreateBackup:=False
        'ActiveWorkbook.Close False
        '
        'Oculta nuevamente las hojas
        If m > -1 Then
            Sheets(HojasOcultas).Visible = 0
        End If
    End If
    Sheets(hojaactiva).Select
    MsgBox "Proceso terminado", vbInformation
End Sub
'
Private Sub ListBox1_Change()
'Por.Dante Amor
    If cargando Then Exit Sub
    cargando = True
    For i = 0 To ListBox1.ListCount - 1
        For j = LBound(hojas) To UBound(hojas)
            If LCase(ListBox1.List(i)) = LCase(hojas(j)) Then
                ListBox1.Selected(i) = True
                Exit For
            End If
        Next
    Next
    cargando = False
End Sub
'
Private Sub UserForm_Activate()
'Por.Dante Amor
    'Hojas que siempre se deben considerar
    hojas = Array("Hoja A", "Hoja B")
    cargando = True
    ListBox1.MultiSelect = 1
    ListBox1.ListStyle = 1
    For Each h In Sheets
        Select Case Left(UCase(h.Name), 2)
            Case "AC", "AS" 'Poner las 2 primeras letras de las
                            'hojas que no deben aparecer en la lista
            '
            Case Else
                ListBox1.AddItem h.Name
                For j = LBound(hojas) To UBound(hojas)
                    If LCase(h.Name) = LCase(hojas(j)) Then
                        ListBox1.Selected(ListBox1.ListCount - 1) = True
                        Exit For
                    End If
                Next
        End Select
    Next
    cargando = False
End Sub

Dante puede ser que no me funcione la macro porque las hojas tienen encabezado y pie, ¿o las hojas tienen que cumplir algún criterio?

Lo que hace el formulario es imprimir las hojas que seleccionaste, no importa si tienen encabezado y pie.

Te envía algún error, ¿o solamente no las imprime?

No las imprime ni las convierte a pdf. No me envía ningún error

Gracias dante por contestar

¿Puedes probar la macro en un libro nuevo con 2 o 3 hojas?

Respuesta
1

[Hola 

Entonces puedes usar esto

https://www.gerencie.com/enviar-varias-hojas-de-un-libro-de-excel-a-imprimir-o-archivo.html

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas