Llenar la columna 11 de un ListBox.

Sé que por defecto un ListBox admite un máximo de 10 columnas. Pero también sé que se le pueden añadir más de 10 por código.

El caso es que para 10 columnas me funciona más que correctamente -por lo que no quisiera cambiarlo-, el siguiente código generador de un ListBox, que, por otro lado, utilizo hasta para 7 botones de comando generadores del mismo TestBox, aunque con contenido potencialmente diferente:

Dim HojaBase As Worksheet
Dim Rango As Range
Dim Columna As Integer
Dim i As Integer
Dim Filas As Integer
' LIMPIO LOS OTROS FILTROS DE BÚSQUEDA Y EL PROPIO LISTBOX
Me.TxtBuscar_Fch = Null
Me.txtBuscar_Nom = Null
Me.txtBuscar_Nif = Null
Me.txtBuscar_Sol = Null
Me.txtBuscar_Med = Null
Me.txtBuscar_Obs = Null
Me.Txt_BusquedaGlobal = Null
Me.txtBuscar_Cod.SetFocus
Me.ListBox1.Clear
On Error GoTo ManejadorErrores
Set HojaBase = ThisWorkbook.Sheets("MIS_EXPEDIENTES")
Set Rango = HojaBase.Range("A1").CurrentRegion
    If Me.txtBuscar_Cod.Value = "" Then Exit Sub
    Me.ListBox1.Clear
    Columna = 1
    Filas = Rango.Rows.Count
    With Me.ListBox1
        For i = 2 To Filas
            If VBA.LCase(HojaBase.Cells(i, Columna).Value) Like "*" & VBA.LCase(Me.txtBuscar_Cod.Value) & "*" Then
                .AddItem HojaBase.Cells(i, 1).Value
                .List(Me.ListBox1.ListCount - 1, 1) = HojaBase.Cells(i, 3).Value
                .List(Me.ListBox1.ListCount - 1, 2) = HojaBase.Cells(i, 4).Value
                .List(Me.ListBox1.ListCount - 1, 3) = HojaBase.Cells(i, 5).Value
                .List(Me.ListBox1.ListCount - 1, 4) = HojaBase.Cells(i, 7).Value
                    If HojaBase.Cells(i, 12) = "Sí" Then
                        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 5) = "Sí"
                    ElseIf HojaBase.Cells(i, 12) = "No" Then
                        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 5) = "No"
                    End If
                    If HojaBase.Cells(i, 13) = "Sí" Then
                        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 6) = "Sí"
                    ElseIf HojaBase.Cells(i, 13) = "No" Then
                        Me.ListBox1.List(Me.ListBox1.ListCount - 1, 6) = "No"
                    End If
                .List(Me.ListBox1.ListCount - 1, 7) = HojaBase.Cells(i, 34).Value
                .List(Me.ListBox1.ListCount - 1, 8) = HojaBase.Cells(i, 58).Value
                .List(Me.ListBox1.ListCount - 1, 9) = HojaBase.Cells(i, 59).Value
        '''''' .List(Me.ListBox1.ListCount - 1, 10) = HojaBase.Cells(i, 60).Value      ' ES LA COLUMNA 11 DEL LISTBOX -QUE POR DEFECTO SOLO ADMITE 10-.
            End If
        Next i
End With
Me.Registros.Value = Me.ListBox1.ListCount
Exit Sub
ManejadorErrores:
MsgBox "Ha ocurrido un error: " & Err.Description, vbExclamation, "EXPEDIENTES"

Lógicamente al 'descomentar' la escritura prevista en el código anterior para la columna 11 del ListBox, en tanto en cuanto no haya definido antes un 'Rango' superior a 10 columnas, error... Pero no sé cómo hacerlo...

¿Alguien me puede echar una mano...?

1 respuesta

Respuesta
1

Visita:

Cursos de Excel y Macros - YouTube


De la siguiente manera se pueden agregar más de 10 columnas, utilizando el método .AddItem:

Primero se establece con la propiedad .List el número de columnas:

Private Sub UserForm_Activate()
  ReDim b(1 To 1, 1 To 20)  'número de columnas a agregar
  With ListBox1
    .ColumnCount = 20       'número de columnas a agregar
    .List = b               'se habilitan las columnas en el list
    .Clear                  'se limpia el list
  End With
End Sub

Y posteriormente puedes utilizarlo así:

Private Sub CommandButton1_Click()
  With ListBox1
    .AddItem Cells(1, 1)
    .List(.ListCount - 1, 1) = Cells(1, 2)
    .List(.ListCount - 1, 2) = Cells(1, 3)
    .List(.ListCount - 1, 3) = Cells(1, 4)
    .List(.ListCount - 1, 4) = Cells(1, 5)
    .List(.ListCount - 1, 5) = Cells(1, 6)
    .List(.ListCount - 1, 6) = Cells(1, 7)
    .List(.ListCount - 1, 7) = Cells(1, 8)
    .List(.ListCount - 1, 8) = Cells(1, 9)
    .List(.ListCount - 1, 9) = Cells(1, 10)
    .List(.ListCount - 1, 10) = Cells(1, 11)
    .List(.ListCount - 1, 11) = Cells(1, 12)
    .List(.ListCount - 1, 12) = Cells(1, 13)
    .List(.ListCount - 1, 13) = Cells(1, 14)
    .List(.ListCount - 1, 14) = Cells(1, 15)
    .List(.ListCount - 1, 15) = Cells(1, 16)
    .List(.ListCount - 1, 16) = Cells(1, 17)
    .List(.ListCount - 1, 17) = Cells(1, 18)
    .List(.ListCount - 1, 18) = Cells(1, 19)
    .List(.ListCount - 1, 19) = Cells(1, 20)
  End With
End Sub

Vídeos recomendados:

Generar archivos en automatico . curso de macros . excel . macros - YouTube

Generar archivo y enviar correo en automatico. #curso de excel #curso de macros #excel #macros - YouTube

Sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas