Problemas con listbox en VBA excel

tengo dos form:

- en form entrada, elijo un valor de cada listbox (son 5) y lo asigno a una celda

- en form modificar, deseo recuperar los valores ingresados, mostrarlos en el mismo formato que se ingresaron y ofrecer la oportunidad de cambiarlo

funciona en 3 de 5 de ellos, a pesar que están las instrucciones están codificadas exactamente igual, solo cambié el nombre del list box

Ingreso de datos

Private Sub UserForm_activate()

supervisor.Clear
Ubic = "a2"
Range(Ubic).Select
Do While ActiveCell <> Empty
supervisor.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
perforadora.Clear
Ubic = "b2"
Range(Ubic).Select
Do While ActiveCell <> Empty
perforadora.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
perforista.Clear
Ubic = "c2"
Range(Ubic).Select
Do While ActiveCell <> Empty
perforista.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
pozo.Clear
Ubic = "d2"
Range(Ubic).Select
Do While ActiveCell <> Empty
pozo.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
proyecto.Clear
Ubic = "e2"
Range(Ubic).Select
Do While ActiveCell <> Empty
proyecto.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop


End Sub

Modificar datos 

Private Sub UserForm_activate()
supervisor.Clear
Ubic = "a2"
Range(Ubic).Select
Do While ActiveCell <> Empty
supervisor.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
perforadora.Clear
Ubic = "b2"
Range(Ubic).Select
Do While ActiveCell <> Empty
perforadora.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
perforista.Clear
Ubic = "c2"
Range(Ubic).Select
Do While ActiveCell <> Empty
perforista.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
pozo.Clear
Ubic = "d2"
Range(Ubic).Select
Do While ActiveCell <> Empty
pozo.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop
proyecto.Clear
Ubic = "e2"
Range(Ubic).Select
Do While ActiveCell <> Empty
proyecto.AddItem ActiveCell
ActiveCell.Offset(1, 0).Select
Loop

For i = 0 To supervisor.ListCount - 1
If supervisor.List(i) = Worksheets("hoja1").Cells(15, 1).Value Then
supervisor.Selected(i) = True
End If
Next i
For i = 0 To perforadora.ListCount - 1
If perforadora.List(i) = Worksheets("hoja1").Cells(15, 2).Value Then
perforadora.Selected(i) = True
End If
Next i
For i = 0 To perforista.ListCount - 1
If perforista.List(i) = Worksheets("hoja1").Cells(15, 3).Value Then
perforista.Selected(i) = True
End If
Next i
For i = 0 To pozo.ListCount - 1
If pozo.List(i) = Worksheets("hoja1").Cells(15, 4).Value Then
pozo.Selected(i) = True
End If
Next i
For i = 0 To proyecto.ListCount - 1
If proyecto.List(i) = Worksheets("hoja1").Cells(15, 5).Value Then
proyecto.Selected(i) = True
End If
Next i

End Sub


Private Sub supervisor_Click()
Worksheets("hoja1").Range("a15").Select
ActiveCell.Value = supervisor
End Sub
Private Sub perforadora_Click()
Worksheets("hoja1").Range("b15").Select
ActiveCell.Value = perforadora
End Sub

Private Sub perforista_Click()
Worksheets("hoja1").Range("c15").Select
ActiveCell.Value = perforista
End Sub

Private Sub pozo_Click()
Worksheets("hoja1").Range("d15").Select
ActiveCell.Value = pozo
End Sub

Private Sub proyecto_Click()
Worksheets("hoja1").Range("e15").Select
ActiveCell.Value = proyecto
End Sub

Respuesta
1

El problema me pasó en los list perforista y pozo; lo solucioné consultando con el método List,

Tenías esto:

ActiveCell.Value = perforista

Lo cambié por esto:

ActiveCell.Value = perforista.List(perforista.ListIndex)


Te anexo el código actualizado con todos los list.

Private Sub supervisor_Click()    Worksheets("hoja1").Range("a15").Select    ActiveCell.Value = supervisor.List(supervisor.ListIndex)End Sub'Private Sub perforadora_Click()    Worksheets("hoja1").Range("b15").Select    ActiveCell.Value = perforadora.List(perforadora.ListIndex)End Sub'Private Sub perforista_Click()    Worksheets("hoja1").Range("c15").Select    ActiveCell.Value = perforista.List(perforista.ListIndex)End Sub'Private Sub pozo_Click()    Worksheets("hoja1").Range("d15").Select    ActiveCell.Value = pozo.List(pozo.ListIndex)End Sub'Private Sub proyecto_Click()    Worksheets("hoja1").Range("e15").Select    ActiveCell.Value = proyecto.List(proyecto.ListIndex)End Sub

Saludos.Dante Amor

Dante,

Excelente, cambié la instrucción en las cinco líneas y funciona perfecto pero, this is crazy, ahora no selecciona en los listbox.

Me explico, al ejecutar se seleccionaba (highlight) en los listbox el valor que tenía la celda, ahora ya no lo hace.

el cambio que hice no tiene nada que ver con esto, ¿como pudo pasar?

Si al inicio de la macro, en la fila 15 no tienes datos, no se seleccionan los datos en los listbox, lo que tienes que hacer es ejecutar el formulario modificar y seleccionar por primera vez unos datos. Ahora sí, la fila 15 ya tiene datos, entra nuevamente al formulario modificar y te debe seleccionar en los listbox los datos que están en la fila15.

Si no te resulta, tendría que revisar cómo están tus datos, en la macro tienes una hoja llamada "hoja1", pero no sé si estás trabajando con más hojas, ya que en esta parte hace la carga del listbox, pero no sé si estás en hoja1 o en otra hoja.

 Supervisor. Clear
    ubic = "a2"
    Range(ubic).Select
    Do While ActiveCell <> Empty
    supervisor.AddItem ActiveCell
    ActiveCell.Offset(1, 0).Select
    Loop

Yo hice pruebas de tu código y si estás en la misma hoja1, se cargan los datos en los listbox, siempre y cuando no lleguen a la fila 15 , ya que estarían encimándose cuando pasas los datos del list a la fila 15.


Otro detalle, en tu código puedes agregar un exit for, en esta parte, si ya encontró el dato, entonces que se salga del for.

    For i = 0 To supervisor.ListCount - 1
        If supervisor.List(i) = Worksheets("hoja1").Cells(15, 1).Value Then
            supervisor.Selected(i) = True
            Exit For
        End If
    Next i

Dante,

Gracias.

La codificación que hice es un extracto del proyecto que desarrollo. Lo hice para aislar el problema por lo que los listbox sólo pueden tener datos hasta la fila 14 como indicas y sólo uso uso la hoja "hoja1"

Tienes razón, si en la fila 15 no hay, no hay nada que seleccionar.

el problema es que habiendo dato, siendo todos los listbox reconocidos y el dato seleccionado (highlighted) en un listbox particular no lo reconoce y lo asigna "", para mi, sin razón alguna. Me resisto a decir (como me han dicho a menudo) "excel está malo" :)

Puede ser que el valor que tienes en la fila 15 no sea igual a alguno de los datos que tienes en las filas de la 1 a la 14, puede ser algún espacio o algún carácter que tenga el dato.

Yo hice pruebas con datos controlados y me funciona muy bien. Intenta con otros datos

Saludos. Dante Amor

Dante,

Gracias, como te dije antes, es un tema de locos, debería funcionar como te funciona a ti... me podrías mandar el archivo que tu usaste?

No creo que sean los datos... ingresé números para hacer más seguro el tema de blancos, mayúsculas, etc. 

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas