Pasar para imprimir solo ciertos items seleccionados en listbox

He modificado una macro de la autoria de Dante con otras macros, que pasa los items del lisbox. Pwro tengo el inconveniente que cuando requiero pasar solo los items que selecciono. Es decir carga 50 items pero solo ocupo 10 por decir algo y al ejecutar los pasa todos. Otro inconveniente es que si no selecciono ninguno crea la hoja y los encabezados y luego envía el mensaje que no hay datos a imprimir.

Tengo activo fmmultiselectmulti

Agradecería su ayuda.

Private Sub CommandButton3_Click()
Set h = Worksheets.Add
h.Cells.Clear
h.Range("A1:H1") = Array("N° CHEQUE", "DOCUMENTO N°", "FECHA", "MONTO", "GIRADO A", "CONCEPTO", "BANCO", "OPERACION")
h.Range("A2:h200").NumberFormat = "General"
For x = 0 To Listbox1.ListCount - 1
If Listbox1.Selected(x) = True Then
For i = 0 To Listbox1.ListCount - 1
h.Cells(i + 2, "A") = Listbox1.List(i, 0)
h.Cells(i + 2, "B") = Listbox1.List(i, 1)
h.Cells(i + 2, "C") = Listbox1.List(i, 2)
h.Cells(i + 2, "D") = Listbox1.List(i, 3)
h.Cells(i + 2, "E") = Listbox1.List(i, 4)
h.Cells(i + 2, "F") = Listbox1.List(i, 5)
h.Cells(i + 2, "G") = Listbox1.List(i, 7)
h.Cells(i + 2, "H") = Listbox1.List(i, 8)
Call formatlistbox
Next i
End If
Next x
u = h.Range("A" & Rows.Count).End(xlUp).Row
If u = 1 Then
MsgBox "No hay registros a imprimir"
Else
h.PrintOut Copies:=1, Collate:=True
ActiveWindow.SelectedSheets.Delete
End If
End Sub

1 respuesta

Respuesta
2

Te anexo la macro actualizada

Private Sub CommandButton3_Click()
    Set h = Worksheets.Add
    h.Range("A1:H1") = Array("N° CHEQUE", "DOCUMENTO N°", "FECHA", "MONTO", "GIRADO A", "CONCEPTO", "BANCO", "OPERACION")
    h.Range("A2:h200").NumberFormat = "General"
    fila = 2
    For x = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(x) = True Then
            h.Cells(fila, "A") = ListBox1.List(x, 0)
            h.Cells(fila, "B") = ListBox1.List(x, 1)
            h.Cells(fila, "C") = ListBox1.List(x, 2)
            h.Cells(fila, "D") = ListBox1.List(x, 3)
            h.Cells(fila, "E") = ListBox1.List(x, 4)
            h.Cells(fila, "F") = ListBox1.List(x, 5)
            h.Cells(fila, "G") = ListBox1.List(x, 7)
            h.Cells(fila, "H") = ListBox1.List(x, 8)
            fila = fila + 1
            Call formatlistbox
        End If
    Next x
    u = h.Range("A" & Rows.Count).End(xlUp).Row
    If u = 1 Then
        MsgBox "No hay registros a imprimir"
    Else
        h.PrintOut Copies:=1, Collate:=True
    End If
    Application.DisplayAlerts = False
    h.Delete
End Sub
'


'.[Sal u dos. Dante Amor. No olvides valorar la respuesta. 
'.[Avísame cualquier duda

¡Gracias! 

Dante la corrección. que hiciste funciono a la perfección. mil gracias.

Analizando la corrección, creo entender el error estaba en la declaración de las variables.

No sé a qué te refieres con "en la declaración de variables".

En tu código tenías 2 ciclos, mientras que en mi código solamente se requiere de un ciclo.

Lee el siguiente artículo sobre variables:

Según su función ¿Cómo declarar variables?

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas