Error al hacer click en listbox

Ten un código para que al hacer click en un list box se refleje la información en todos los controles de mi formulario pero me esta arrojando el error que ven en la imagen como puedo corregirlo? Este formulario funcionaba bien algo modifique sin darme cuenta.

Private Sub ListBox1_Click()
With ListBox1
  If .ListIndex = -1 Then Exit Sub
   TextBox1 = .List(.ListIndex, 2)
   TextBox8 = CDate(.List(.ListIndex, 2)) 'FECHA DE ENTREGA
   TextBox9 = Format(.List(.ListIndex, 3), "hh:mm:ss am/pm") 'HORA DE ENTRGA
   TextBox15 = CDate(.List(.ListIndex, 17)) 'Fecha devolución
   TextBox14 = Format(.List(.ListIndex, 18), "hh:mm:ss am/pm") 'Hora devolución
   TextBox10 = CDate(.List(.ListIndex, 19)) 'Fecha recepción corrección
   TextBox11 = Format(.List(.ListIndex, 20), "hh:mm:ss am/pm") 'Hora recepcion corrección
   TextBox12 = CDate(.List(.ListIndex, 22)) 'Fecha entrega GC
   TextBox13 = Format(.List(.ListIndex, 23), "hh:mm:ss am/pm") 'Hora entrega GC
   TextBox16 = .List(.ListIndex, 21) 'Transito correccion
   TextBox17 = .List(.ListIndex, 24) 'Transito total
   TextBox18 = "Registrado por:" & "  " & .List(.ListIndex, 7) & "   " & "Modicado por:" & .List(.ListIndex, 27) ' Registro de modificaciones
   TextBox2 = .List(.ListIndex, 15) 'OBSERVACIONES
  ComboBox1 = .List(.ListIndex, 5) 'PRODUCTO1
  Controls(.List(.ListIndex, 6)) = True
  Controls(.List(.ListIndex, 4)) = True
  Controls(.List(.ListIndex, 16)) = True
  TextBox3 = .List(.ListIndex, 8) 'DESV 1
  TextBox4 = .List(.ListIndex, 9) 'DESV 2
  TextBox5 = .List(.ListIndex, 10) 'DESV 3
  TextBox6 = .List(.ListIndex, 11) 'DESV 4
  TextBox7 = .List(.ListIndex, 12) 'DESV 5
  If .List(.ListIndex, 13) = "1" Then Devuelto.Value = True Else Devuelto.Value = False
  If .List(.ListIndex, 14) = "1" Then RNC.Value = True Else RNC.Value = False
End With
End Sub

1 Respuesta

Respuesta
1

Revisa el número de columnas que tienes en tu listbox.

Revisa la propiedad .ColumnCount, debes tener por lo menos 25 columnas

También revisa cómo estás cargando el listbox, supongo que utilizas rowsource, también debes tener por lo menos 25 columnas, es decir, debes tener un .Rowsource desde A y hasta la columna Y (por lo menos)

Revisa y me comentas

[

Hola mira tengo 28 columnas

aun no me funciona bien te anexo el código de búsqueda y el inicialización del formulario donde tengo definidas las propiedades

Private Sub UserForm_Initialize()
Sheets("Datos").Select
TextBox8 = Date
TextBox9 = Format(Time, "hh:mm:ss am/pm")
TextBox10 = Date
TextBox10.Enabled = False
TextBox11 = Format(Time, "hh:mm:ss am/pm")
TextBox11.Enabled = False
TextBox12 = Date
TextBox12.Enabled = False
TextBox13 = Format(Time, "hh:mm:ss am/pm")
TextBox13.Enabled = False
TextBox15 = Date
TextBox15.Enabled = False
TextBox14 = Format(Time, "hh:mm:ss am/pm")
TextBox14.Enabled = False
TextBox16.Value = ""
TextBox16.Enabled = False
TextBox17.Enabled = False
'Format(Time, "hh:mm:ss am/pm")
With ListBox1     '1;02;03;04;05;006;07;08;910111213;14;1516;17;18;19;020;21;22;23;24;25;26;27;28
  .ColumnCount = 28
  .ColumnWidths = "0;45;66;55;45;200;0;0;0;0;0;0;0;0;0;0;45;78;69;115;64;80;90;80;62;100;0;0"
  .ColumnHeads = True
  DoEvents
End With
Set ws1 = Sheets("Datos")
If ws1.[b3] = "" Then Exit Sub
With ws1.Range(ws1.[b3], ws1.[b2].End(xlDown)).Offset(, -1)
  .Formula = "=row()"
  .Value = .Value
End With
Fases = Array("Envase", "Empaque")
Entregas = Array("Total", "Parcial")
Turnos = Array("Primero", "Segundo", "SobreTiempo", "Feriado")
'TextBox19 = "1"
'TextBox19 = ""
End Sub
Private Sub TextBox19_Change()
'Buscar por lote
'===================
On Error Resume Next
Set b = Sheets("Datos") 'Cambiar aqui el "NOMBRE" de la hoja criterio de bsuqueda
uf = b.Range("A" & Rows.Count).End(xlUp).Row
If Trim(TextBox19.Value) = "" Then
     Me.ListBox1.List() = b.Range("A2:AA" & uf).Value
     Me.ListBox1.ColumnHeads = True
     Me.ListBox1.RowSource = "Datos!A2:AA" & uf 'Cambiar aqui el "NOMBRE" de la hoja criterio de bsuqueda
   Exit Sub
End If
b.AutoFilterMode = False
Me.ListBox1 = Clear
Me.ListBox1.RowSource = Clear
For i = 2 To uf
   strg = b.Cells(i, 2).Value 'Aqui cambio la "COLUMNA" CRITERIO DE BUSQUEDA
   If UCase(strg) Like UCase(TextBox19.Value) & "*" Then 'Aqui se agregan items al listbox
       Me.ListBox1.ColumnCount = 29
       Me.ListBox1.ColumnHeads = True
       Me.ListBox1.AddItem b.Cells(i, 1)
       Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = b.Cells(i, 2)
       Me.ListBox1.List(Me.ListBox1.ListCount - 1, 2) = b.Cells(i, 3)
       Me.ListBox1.List(Me.ListBox1.ListCount - 1, 3) = b.Cells(i, 4)
       Me.ListBox1.List(Me.ListBox1.ListCount - 1, 4) = b.Cells(i, 5)
       Me.ListBox1.List(Me.ListBox1.ListCount - 1, 5) = b.Cells(i, 6)
       Me.ListBox1.List(Me.ListBox1.ListCount - 1, 6) = b.Cells(i, 7)
       Me.ListBox1.List(Me.ListBox1.ListCount - 1, 7) = b.Cells(i, 8)
   End If
Next i
Me.ListBox1.ColumnWidths = "0;45;66;55;45;200;0;0;0;0;0;0;0;0;0;0;45;0;0;0;0;0;0;0;0;0;0;0"
End Sub

Este es el formulario que le comente en otras preguntas me gustaría pasarle para que lo revise esta digo yo 90% completado.

Pues tienes varios problemas.

Primero. En esta línea le estás diciendo del evento TextBox19_Change:

Me.ListBox1.RowSource = "Datos!A2:AA" & uf

De la columna A a la AA solamente hay 27 columnas. Eso significa que en el listbox se cargan desde la posición 0 hasta la 26.

Luego, en esta línea del evento ListBox1_Click:

TextBox18 = "Registrado por:" & "  " & .List(.ListIndex, 7) & "   " & "Modicado por:" & .List(.ListIndex, 27)

Tienes .ListIndex, 27, en tu listbox solamente tienes hasta la columna 26 (no olvidar que la columna 0 también cuenta).

Si realmente quieres cargar 28 columnas entonces tienes que modificar el .RowSource, así:

Me.ListBox1.RowSource = "Datos!A2:AB" & uf 

Con eso solucionas parte de tu problema.


El otro problema que tienes; y es realmente grave, es lo siguiente. En el evento TextBox19_Change, estás agregando registros con .AddItem, pero ahí solamente estás agregando 8 columnas (de la 0 a la 7)

           Me.ListBox1.AddItem b.Cells(i, 1)
           Me.ListBox1.List(Me.ListBox1.ListCount - 1, 1) = b.Cells(i, 2)
           Me.ListBox1.List(Me.ListBox1.ListCount - 1, 2) = b.Cells(i, 3)
           Me.ListBox1.List(Me.ListBox1.ListCount - 1, 3) = b.Cells(i, 4)
           Me.ListBox1.List(Me.ListBox1.ListCount - 1, 4) = b.Cells(i, 5)
           Me.ListBox1.List(Me.ListBox1.ListCount - 1, 5) = b.Cells(i, 6)
           Me.ListBox1.List(Me.ListBox1.ListCount - 1, 6) = b.Cells(i, 7)
           Me.ListBox1.List(Me.ListBox1.ListCount - 1, 7) = b.Cells(i, 8)

Entonces cuando seleccionas un registro en el listbox vas a tener error en esta línea:

TextBox15 = CDate(. List(. ListIndex, 17))

Estás queriendo poner la columna 17, pero solamente cargaste 8.


Ese es el problema, supongo que quieres una solución, pero si cargas con .AddItem, solamente tendrás hasta 10 columnas. Debes cargar nuevamente con la propiedad .RowSource.

Una solución es que copies los registros que coinciden a otra hoja y luego cargues la información de esa hoja.

If UCase(strg) Like UCase(TextBox19.Value) & "*" Then

Revisa la siguiente respuesta, ahí paso los datos a la hoja "Temporal" y después cargo

[Sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas