3 Desplegables condicionados con celdas combinadas en un formulario de Excel

Dante Amor me ayudo con un código y me gustaría trabajar sobre el pero no logro entenderlo.

¿Me podríais explicar como actúa cada linea para poder trabajar con este código? Lo que quiero conseguir es que haya un tercer combobox condicionado (llamado especific2).

Ej: Mobiliario (generic "combobox1") -> Mesa (especific "combobox2") -> "modelo de la mesa" (especific2 "combobox3")

La columna A y B hay celdas combinadas (forman grupos). Abajo del código adjunto una captura de los datos.

Nombre de los COMBOBOX

Generic -> combobox1

Especific-> combobox2

Especific2-> combobox3

Private Sub UserForm_Initialize()
    generic.Clear
    Set h2 = Sheets("full2") 'estableces en el objeto h2 la hoja2,
    For i = 1 To h2.Range("A" & Rows.Count).End(xlUp).Row
        If h2.Cells(i, "A").Value <> "" Then
            generic.AddItem h2.Cells(i, "A")
        End If
    Next
End Sub
'
Private Sub generic_Change()
'Por Dante Amor
    especific.Clear
    If generic.Value = "" Or generic.ListIndex = -1 Then
        Exit Sub
    End If
    '
    Set h2 = Sheets("full2")
    Set b = h2.Columns("A").Find(generic.Value, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
    If Not b Is Nothing Then
        If b.MergeCells Then
            ini = b.MergeArea.Cells(1, 1).Row
            fin = b.MergeArea.Rows.Count + ini - 1
            For i = ini To fin
                especific.AddItem h2.Cells(i, "B")
            Next
        Else
            especific.AddItem h2.Cells(b.Row, "B")
        End If
    End If
End Sub

2 Respuestas

Respuesta
2

Como te había comentado el código se hace un poco más complejo al tener celdas combinadas. Sería más práctico si utilizas tu base de esta forma:

Normalmente así es como se trabaja con una base de datos, de esa forma es más práctico, crear, insertar filas, consultar, modificar o eliminar registros.

Incluso cada que creas un registro tienes que estar ajustando las celdas combinados.


El código para 3 combos en una base de datos de ese tipo quedaría así:

Private Sub ComboBox1_Change()
    ComboBox2. Clear
    ComboBox3. Clear
    Set h2 = Sheets("Hoja2")
    For i = 2 To h2.Range("A" & Rows.Count).End(xlUp).Row
        If h2.Cells(i, "A").Value = ComboBox1.Value Then
            Call Agregar(ComboBox2, h2.Cells(i, "B"))
        End If
    Next
End Sub
'
Private Sub ComboBox2_Change()
    ComboBox3.Clear
    Set h2 = Sheets("Hoja2")
    For i = 2 To h2.Range("A" & Rows.Count).End(xlUp).Row
        If h2.Cells(i, "A").Value = ComboBox1.Value And _
           h2.Cells(i, "B").Value = ComboBox2.Value Then
            Call Agregar(ComboBox3, h2.Cells(i, "C"))
        End If
    Next
End Sub
'
Private Sub UserForm_Activate()
    Set h2 = Sheets("Hoja2")
    For i = 2 To h2.Range("A" & Rows.Count).End(xlUp).Row
        Call Agregar(ComboBox1, h2.Cells(i, "A"))
    Next
End Sub
'
Sub Agregar(combo As ComboBox, dato As String)
'por.DAM agrega los item únicos y en orden alfabético
    For i = 0 To combo.ListCount - 1
        Select Case StrComp(combo.List(i), dato, vbTextCompare)
            Case 0: Exit Sub 'ya existe en el combo y ya no lo agrega
            Case 1: combo.AddItem dato, i: Exit Sub 'Es menor, lo agrega antes del comparado
        End Select
    Next
    combo.AddItem dato 'Es mayor lo agrega al final
End Sub

Prueba y me comentas

.

.

Respuesta
1

Hasta tanto tengas la respuesta de dante hecha un vistazo al link, te puede ayudar.

https://youtu.be/KCHAk9-dAPM

https://youtu.be/7c7mV8fe4sw

visita https://programarexcel.com

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas