Pasar items de un listbox a otro

Soy nueva programando y necesito pasar items de un listbox a otro y que al pasarse se borren del primero hacer una lista y después pasar a una hoja de excel, tengo el siguiente código:

Private Sub CommandButton3_Click()
ListBox2.AddItem ListBox1.Value
For m = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(m) = True Then
ListBox1.RemoveItem m
End If
Next
End Sub
Private Sub CommandButton4_Click()
ListBox1.AddItem ListBox2.Value
For p = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(p) = True Then
ListBox2.RemoveItem m
End If
Next
End Sub

Private Sub UserForm_Initialize()
Sheets("ALUMNOS").Select
Range("A1").Select
Do While ActiveCell.Value <> ""
ListBox1.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop

Dim fila As Long
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set b = Sheets("ALUMNOS")
UF = b.Range("A" & Rows.Count).End(xlUp).Row
uc = b.Cells(1, Columns.Count).End(xlToLeft).Address
wc = Mid(uc, InStr(uc, "$") + 1, InStr(2, uc, "$") - 2)
With Me.ListBox1
.ColumnCount = 4
.ColumnWidths = "50 pt;180 pt;60 pt;60 pt;60 pt;40 pt;90 pt;50pt;60pt"
.RowSource = "ALUMNOS!A2:" & wc & UF
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True

Me.txt_fch.Value = Date
Me.txt_hr.Value = Time
End Sub

Me aparece un error en "ListBox1.RemoveItem m", ¿alguien puede ayudarme?

1 respuesta

Respuesta
1

Te comento varios detalles.

- Cuando inicias el userform, cargas el listbox1 con additem.

    Do While ActiveCell.Value <> ""
        ListBox1.AddItem ActiveCell
        ActiveCell.Offset(1, 0).Select
    Loop

- Después cargas nuevamente el listbox1 pero con rowsource:

    With Me.ListBox1
        .ColumnCount = 4
        .ColumnWidths = "50 pt;180 pt;60 pt;60 pt;60 pt;40 pt;90 pt;50pt;60pt"
        .RowSource = "ALUMNOS!A2:" & wc & UF
    End With

- Si vas a borrar un registro del listbox con RemoveItem, entonces NO puedes cargar con RowSource.

- Para borrar con RemoveItem, entonces tienes que cargar con AddItem. Por lo que veo, es que necesitas cargar 4 columnas. Entonces debería quedar así todo tu código:

Private Sub CommandButton3_Click()
'Act.Por.Dante Amor
    'Del listbox1 -> listbox2
    If ListBox1.ListIndex = -1 Then Exit Sub
    reg = ListBox1.ListIndex
    ListBox2. AddItem ListBox1. List(reg, 0)
    ListBox2. List(ListBox2.ListCount - 1, 1) = ListBox1. List(reg, 1)
    ListBox2. List(ListBox2.ListCount - 1, 2) = ListBox1. List(reg, 2)
    ListBox2. List(ListBox2.ListCount - 1, 3) = ListBox1. List(reg, 3)
    ListBox1. RemoveItem reg
End Sub
'
Private Sub CommandButton4_Click()
'Act.Por.Dante Amor
    'Del listbox2 -> listbox1
    If ListBox2.ListIndex = -1 Then Exit Sub
    reg = ListBox2.ListIndex
    ListBox1. AddItem ListBox2. List(reg, 0)
    ListBox1. List(ListBox1.ListCount - 1, 1) = ListBox2. List(reg, 1)
    ListBox1. List(ListBox1.ListCount - 1, 2) = ListBox2. List(reg, 2)
    ListBox1. List(ListBox1.ListCount - 1, 3) = ListBox2. List(reg, 3)
    ListBox2. RemoveItem reg
End Sub
'
Private Sub UserForm_Initialize()
'Act.Por.Dante Amor
    '
    Dim fila As Long
    Set h = Sheets("ALUMNOS")
    ListBox1.ColumnCount = 4
    ListBox2.ColumnCount = 4
    fila = 2
    Do While h.Cells(fila, "A").Value <> ""
        ListBox1.AddItem h.Cells(fila, "A").Value
        ListBox1.List(ListBox1.ListCount - 1, 1) = h.Cells(fila, "B")
        ListBox1.List(ListBox1.ListCount - 1, 2) = h.Cells(fila, "C")
        ListBox1.List(ListBox1.ListCount - 1, 3) = h.Cells(fila, "D")
        fila = fila + 1
    Loop
    '
    Me.txt_fch.Value = Date
    Me.txt_hr.Value = Time
End Sub

No pusiste la parte de pasar los datos a la hoja, no sé si vas a pasar el listbox1 o el 2 y a cuál hoja?

Con gusto también te ayudo con esa parte, valora esta respuesta y crea una nueva pregunta en el tema de microsoft Excel. En el desarrollo de la pregunta escribe: "para Dante Amor". Ahí me describes con detalle lo que necesitas.

Sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas