¿Cómo eliminar registros en 2 listbox simultáneamente y respetar su orden?

Amigos la pregunta es:

Tengo dos listbox: listbox1 y listbox2. Cada que creo un registro en el listbox1 se agrega otro automáticamente en el listbox2. El problema es que necesito que cuando elimine un registro en el listbox1 se elimine también en el listbox2 el mismo registro que se creo cuando cree el registro del listbox1.

He intentado con el código que aparece a continuación y puedo borrar los dos registros en simultanea pero al seleccionar un registro del listbox1 borra siempre el registro que aparezca en la primera intentar del listbox2 y lo que necesito es que borre el registro que le corresponde en el listbox2. Es decir no he podido lograr que borre los registros en simultanea pero respetando el orden.

Private Sub CommandButton6_Click()

'ELIMINAR listbox1

Dim Cuenta As Integer
Dim Numero As Integer
Dim j As Integer
Dim i As Integer
Dim strNombreItem As String
Dim TotalSeleccionado As Double
Dim TOTALPLSELECCIONADO As Double
'
Cuenta = Me.ListBox1.ListCount
Numero = 0
'
'Validamos que haya un elemento seleccionado.
For j = 0 To Cuenta - 1
If Me.ListBox1.Selected(j) = True Then
Me!ListBox2.Selected(g) = True
Numero = Numero + 1
End If
Next j
'
If Numero <> 0 Then
'
For i = 0 To Cuenta - 1
If Me.ListBox1.Selected(i) = True Then
strNombreItem = Me.ListBox1.List(i)
TotalSeleccionado = ListBox1.List(i, 7)
TOTALPLSELECCIONADO = ListBox1.List(i, 4)
Me.ListBox1.RemoveItem i
End If
Next i
'
End If

'ELIMINAR listbox2

Dim Cuenta2 As Integer
Dim Numero2 As Integer

Dim y As Integer
Dim strNombreItem2 As String
Dim TotalSeleccionado2 As Double
Dim TOTALPLSELECCIONADO2 As Double
'
Cuenta2 = Me.ListBox2.ListCount
Numero2 = 0
'
'Validamos que haya un elemento seleccionado.
For g = 0 To Cuenta - 1
If Me.ListBox2.Selected(g) = True Then
Numero2 = Numero2 + 1
End If
Next g
'
If Numero2 <> 0 Then
'
'La hoja seleccionada se pasará al ListBox de hojas visibles.
For y = 0 To Cuenta2 - 1
If Me.ListBox2.Selected(y) = True Then
strNombreItem2 = Me.ListBox2.List(y)
'TotalSeleccionado2 = ListBox2.List(y, 7)
'TOTALPLSELECCIONADO2 = ListBox1.List(y, 4)
Me.ListBox2.RemoveItem y
End If
Next y
'
End If
End Sub

1 Respuesta

Respuesta
2

Tiene algunos detalles tu código.

Por ejemplo en esta parte:

'Validamos que haya un elemento seleccionado.
For j = 0 To Cuenta - 1
    If Me.ListBox1.Selected(j) = True Then
    Me!ListBox2.Selected(g) = True
    Numero = Numero + 1
    End If
Next j

en esta línea:

Me!ListBox2.Selected(g) = True

después de "Me" debe llevar punto

Y el contador debe ser j y tienes g, debería ser así:

'Validamos que haya un elemento seleccionado.
For j = 0 To Cuenta - 1
    If Me.ListBox1.Selected(j) = True Then
    Me.ListBox2.Selected(j) = True
    Numero = Numero + 1
    End If
Next j

Pero regresando a borrar los registros en los listbox, si ambos listbox son iguales en número de registros, como dices: "Cada que creo un registro en el listbox1 se agrega otro automáticamente en el listbox2"

Y por ejemplo quieres borrar los registros 1 y 5 del listbox1, entonces también se deberían borrar los registros 1 y 5 del listbox2.

El código quedaría así:

Private Sub CommandButton6_Click()
'Eliminar item de listbox1 y listbox2
    For i = ListBox1.ListCount - 1 To 0 Step -1
        If Me.ListBox1.Selected(i) = True Then
            strNombreItem = Me.ListBox1.List(i)
            TotalSeleccionado = ListBox1.List(i, 7)
            TOTALPLSELECCIONADO = ListBox1.List(i, 4)
            strNombreItem2 = Me.ListBox2.List(i)
            ListBox1.RemoveItem i
            ListBox2.RemoveItem i
        End If
    Next
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas