Comparar dato con ítem seleccionado en un Lixtbox que muestra 10 columnas

Tengo un formulario que me carga estos datos en un Listbox

______A___________B_____________C__________D___________E_______

1| Código | Detalle_Titulo | Cantidad | Prestamos | Observaciones

2 | 24073  | Titulo Libro 1 | Cant. L1  | Disponible | Ninguna

3 | 43526  | Titulo Libro 2 | Cant. L2  | No Dispon | 


Como hago para que al seleccionar un ítem del listbox y picar en el botón "CBtn_Solicitar" me detecte si el ítem seleccionado se encuentra o no disponible, osea, que compruebe en ese ítem la columna D que corresponde a estado de prestamos...

1 respuesta

Respuesta
1

H o l a:

¿Podrías poner el código que estás utilizando para cargar los datos en el listbox?

¿Es un listbox de formulario o de control ActiveX?

Que tal Dante Amor, es un listbox de formulario... tengo dos maneras de cargar los datos en el listbox, una es por medio de una búsqueda que realizo, para ello uso este código ()

Private Sub CBtn_BuscarREGISTROS_Click()
    If Me.ListBoxRESULTADOS.ListCount > 0 Then
        ListBoxRESULTADOS.RowSource = ""
    End If
    On Error GoTo Errores
    If Trim(Me.TextBoxBUSQUEDA.Value = "") Then GoTo Errores
    If Trim(Me.cmbEncabezado Is Nothing) Then GoTo Errores
        Columna = Me.cmbEncabezado.ListIndex
        j = 1
        Filas = Range("A1").CurrentRegion.Rows.Count
        For i = 2 To Filas
            If LCase(Cells(i, j).Offset(0, CInt(Columna)).Value) Like "*" & LCase(Me.TextBoxBUSQUEDA.Value) & "*" Then
                Me.ListBoxRESULTADOS.AddItem Cells(i, j)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 1) = Cells(i, j).Offset(0, 1)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 2) = Cells(i, j).Offset(0, 2)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 3) = Cells(i, j).Offset(0, 3)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 4) = Cells(i, j).Offset(0, 4)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 5) = Cells(i, j).Offset(0, 5)
            Else
                MsgBox "Su búsqueda no produjo ningún resultado con el filtro seleccionado." & vbCrLf & _
                        "Intente nuevamente con otro filtro de búsqueda u otro dato.", vbCritical, "Registro no encontrado"
                TextBoxBUSQUEDA.Value = ""
                TextBoxBUSQUEDA.SetFocus
                Exit Sub
            End If
        Next i
    Exit Sub
Errores:
    MsgBox "Compruebe que seleccionó un filtro para la búsqueda" & vbCrLf & _
            "y/o definió un dato a buscar e inténtelo nuevamente", vbCritical, "Error de usuario"
End Sub

y la otra es por medio de un botón que me muestra todos los registros de la base de datos, para esto uso el siguiente código...

' Muestra todos los registros de la base de datos
Private Sub CBtn_VerREGISTROS_Click()
    Dim UltLinea As Long
    UltLinea = Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
    For i = 2 To UltLinea
        ListBoxRESULTADOS.AddItem
        ListBoxRESULTADOS.ColumnHeads = False
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 0) = Cells(i, "A")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 1) = Cells(i, "B")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 2) = Cells(i, "C")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 3) = Cells(i, "D")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 4) = Cells(i, "E")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 5) = Cells(i, "F")
    Next
End Sub

Te anexo las macros actualizadas:

Agrega en la siguiente macro el número de fila en el ListBoxRESULTADOS

Private Sub CBtn_BuscarREGISTROS_Click()
    If Me.ListBoxRESULTADOS.ListCount > 0 Then
        ListBoxRESULTADOS.RowSource = ""
    End If
    On Error GoTo Errores
    If Trim(Me.TextBoxBUSQUEDA.Value = "") Then GoTo Errores
    If Trim(Me.cmbEncabezado Is Nothing) Then GoTo Errores
        Columna = Me.cmbEncabezado.ListIndex
        j = 1
        Filas = Range("A1").CurrentRegion.Rows.Count
        For i = 2 To Filas
            If LCase(Cells(i, j).Offset(0, CInt(Columna)).Value) Like "*" & LCase(Me.TextBoxBUSQUEDA.Value) & "*" Then
                Me.ListBoxRESULTADOS.AddItem Cells(i, j)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 1) = Cells(i, j).Offset(0, 1)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 2) = Cells(i, j).Offset(0, 2)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 3) = Cells(i, j).Offset(0, 3)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 4) = Cells(i, j).Offset(0, 4)
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 5) = Cells(i, j).Offset(0, 5)
                'Agregar número de fila
                Me.ListBoxRESULTADOS.List(Me.ListBoxRESULTADOS.ListCount - 1, 6) = i
            Else
                MsgBox "Su búsqueda no produjo ningún resultado con el filtro seleccionado." & vbCrLf & _
                        "Intente nuevamente con otro filtro de búsqueda u otro dato.", vbCritical, "Registro no encontrado"
                TextBoxBUSQUEDA.Value = ""
                TextBoxBUSQUEDA.SetFocus
                Exit Sub
            End If
        Next i
    Exit Sub
Errores:
    MsgBox "Compruebe que seleccionó un filtro para la búsqueda" & vbCrLf & _
            "y/o definió un dato a buscar e inténtelo nuevamente", vbCritical, "Error de usuario"
End Sub

También en la siguiente macro:

' Muestra todos los registros de la base de datos
Private Sub CBtn_VerREGISTROS_Click()
    Dim UltLinea As Long
    UltLinea = Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
    For i = 2 To UltLinea
        ListBoxRESULTADOS.AddItem
        ListBoxRESULTADOS.ColumnHeads = False
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 0) = Cells(i, "A")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 1) = Cells(i, "B")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 2) = Cells(i, "C")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 3) = Cells(i, "D")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 4) = Cells(i, "E")
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 5) = Cells(i, "F")
        'Agregar número de fila
        ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListCount - 1, 6) = i
    Next
End Sub

Cambia el número de columnas del ListBoxRESULTADOS a 6, de esa forma el número de fila no se verá en el listbox.


Agrega la siguiente macro:

Private Sub CBtn_Solicitar_Click()
'Por.Dante Amor
    If ListBoxRESULTADOS.ListCount = 0 Then
        MsgBox "No hay registros"
        Exit Sub
    End If
    If ListBoxRESULTADOS.ListIndex = -1 Then
        MsgBox "Selecciona un registro"
        Exit Sub
    End If
    '
    fila = ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListIndex, 6)
    MsgBox Cells(fila, "D")
End Sub

S a l u d o s . D a n t e   A m o r. Recuerda valorar la respuesta.

Dante Amor, no era exactamente lo que buscaba, puesto que mi intención era comparar el contenido de la celda y en base a eso me mostrara otro formulario, pero, no puedo negar que me sirvió el código... te muestro como ha quedado

Private Sub CBtn_Solicitar_Click()
    'Por.Dante Amor
    If ListBoxRESULTADOS.ListCount = 0 Then
        MsgBox "No hay registros"
        Exit Sub
    End If
    If ListBoxRESULTADOS.ListIndex = -1 Then
        MsgBox "Selecciona un registro"
        Exit Sub
    End If
    '
    fila = ListBoxRESULTADOS.List(ListBoxRESULTADOS.ListIndex, 6)
'    MsgBox Cells(fila, "E")
    ' He añadido este condición y me va de 10
    If ListBoxRESULTADOS.ListIndex > -1 Then
        If Cells(fila, "E").Value = "Disponible" Then
            Formulario_Prestamos.Show
        Else
            Formulario_InfoPrestamos.Show
        End If
    End If
End Sub

Esa era la idea, darte la fila, para que con ese dato pudieras continuar con lo que necesitabas.

S a l u d o s

La pregunta no admite más respuestas

Más respuestas relacionadas