
ordenar los datos de un listbox
Publicado por Jorge (39 intervenciones) el 17/09/2021 04:16:05
Buenas noches, tengo una traba en mi formulario. Como se puede observar en la imagen, el formulario cuenta con dos ListBox. El primer listbox (ListBox1) se rellena mediante el combobox "unidad" al presionar el botón "buscar", con doble click, puedo pasar datos del listbox1 al listbox2 (listbox de la parte inferior).
En la parte superior hay dos marcos que contienen dos botones de opciones que tienen el propósito de ordenar los datos del listbox1 en forma ascendente o descendente.
Ejemplo: Selecciono la unidad "CZ-35 APURE" y quiero que se ordenen de forma ascendente/descendente por la "Emisión" y de forma ascendente/descendente por la "Nomenclatura".

Anexo código del formulario:
En la parte superior hay dos marcos que contienen dos botones de opciones que tienen el propósito de ordenar los datos del listbox1 en forma ascendente o descendente.
Ejemplo: Selecciono la unidad "CZ-35 APURE" y quiero que se ordenen de forma ascendente/descendente por la "Emisión" y de forma ascendente/descendente por la "Nomenclatura".

Anexo código del formulario:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Private Sub buscar_Click()
Dim DATAPAD, i As Long
DATAPAD = Hoja1.Range("A" & Rows.Count).End(xlUp).Row 'hoja data
ListBox1.Clear
ListBox2.Clear
'Aqui añadimos los datos al listbox1
For i = 2 To DATAPAD + 1
If UCase(Hoja1.Cells(i, 5)) Like "*" & UCase(unidad) & "*" Then
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = Hoja1.Cells(i, 3) 'siglas
.List(.ListCount - 1, 1) = Hoja1.Cells(i, 4) 'involucrado
.List(.ListCount - 1, 2) = Hoja1.Cells(i, 2) 'cedula
.List(.ListCount - 1, 3) = Hoja1.Cells(i, 18) 'Causa
.List(.ListCount - 1, 4) = Hoja1.Cells(i, 22) 'tipo de orden
If Hoja1.Cells(i, 25) = "" Then
.List(.ListCount - 1, 5) = Hoja1.Cells(i, 24) 'nomenclatura
Else
.List(.ListCount - 1, 5) = Hoja1.Cells(i, 25) 'Nro. Oficio (Breve o Nota Informativa)
End If
.List(.ListCount - 1, 6) = Hoja1.Cells(i, 26) 'sustanciador
.List(.ListCount - 1, 7) = Hoja1.Cells(i, 28) 'emision
End With
End If
Next i
resultado = "Se han encontrado " & ListBox1.ListCount & " registros"
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean): On Error Resume Next
Dim Fila
Fila = Me.ListBox1.ListIndex
Me.ListBox2.AddItem ListBox1.List(Fila, 0)
Me.ListBox2.List(ListBox2.ListCount - 1, 1) = ListBox1.List(Fila, 1)
ListBox2.List(ListBox2.ListCount - 1, 2) = ListBox1.List(Fila, 2)
ListBox2.List(ListBox2.ListCount - 1, 3) = ListBox1.List(Fila, 3)
ListBox2.List(ListBox2.ListCount - 1, 4) = ListBox1.List(Fila, 4)
ListBox2.List(ListBox2.ListCount - 1, 5) = ListBox1.List(Fila, 5)
ListBox2.List(ListBox2.ListCount - 1, 6) = ListBox1.List(Fila, 6)
ListBox2.List(ListBox2.ListCount - 1, 7) = ListBox1.List(Fila, 7)
ListBox2.List(ListBox2.ListCount - 1, 8) = ListBox1.List(Fila, 8)
registros = "Se han seleccionado " & ListBox2.ListCount & " registros"
End Sub
'Eliminar un registro seleccionado
'
Private Sub ListBox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If ListBox2.ListIndex >= 0 Then ListBox2.RemoveItem ListBox2.ListIndex
End Sub
'Devuelve la etiqueta del ListBox2 a su valor origen
'
Private Sub unidad_Change()
registros = "Cantidad de registros a enviar al Acta de Entrega"
End Sub
'Añade las unidades a la lista
'
Private Sub UserForm_Initialize()
Dim Lista As Long
Lista = Hoja2.Range("C2").End(xlDown).Row
unidad.RowSource = "opciones!C2:C" & Lista
End Sub
'Reinicia el formulario
'
Private Sub limpiar_Click()
Unload Me
frmActaEntrega.Show
End Sub
Valora esta pregunta


0