Vincular dos combobox y un textbox después del segundo combo box

Antemano aclara soy principiante. Tengo un problema tengo dos combobox (codigos de productos) vinculados el primero del segundo y por otro lado tengo otro combobox ( dodigos de producto) vinculado con otros textbox (descripción del producto), Las dos funciones me trabajan separadas pero cuando las trato de unir me saltan errores.

Código de los dos combobox

Private Sub ComboBox1_Change()

ComboBox6.Clear

Sheets("Codigos").Select

columna1 = ComboBox1.ListIndex + 1

Cells(2, columna1).Select
ultimaFila = Columns("A:A").Range("A100000").End(xlUp).Row
For cont = 2 To ultimaFila

If Cells(cont, columna1) <> "" Then
ComboBox6.AddItem (Cells(cont, columna1))

End If

Next

Sheets("Inicio").Select
End Sub

Private Sub UserForm_Activate()
Sheets("Partida").Select

ultimaFila = Columns("A:A").Range("A100000").End(xlUp).Row
For cont = 2 To ultimaFila

If Cells(cont, 1) <> "" Then
ComboBox1.AddItem (Cells(cont, 1))

End If
Next

Sheets("Inicio").Selec

CODIGO DEL COMBOBOX VINCULADO AL TEXTBOX

Private Sub ComboBox1_Change()
Dim Fila As Integer
Dim Final As Integer

If ComboBox1.Value = "" Then
Me.txt_Nombre = ""
Me.txt_Saldo = ""
End If

For Fila = 2 To 10000
If Hoja2.Cells(Fila, 1) = "" And Hoja5.Cells(Fila, 1) = "" Then
Final = Fila - 1
Exit For
End If
Next
For Fila = 2 To Final
If ComboBox1 = Hoja2.Cells(Fila, 1) Then
Me.txt_Nombre = Hoja2.Cells(Fila, 2)
Exit For
End If
Next
For Fila = 2 To Final
If ComboBox1 = Hoja5.Cells(Fila, 1) Then
Me.txt_Saldo = Hoja5.Cells(Fila, 3)
Exit For
End If
Next

End Sub
Private Sub ComboBox1_Enter()
Dim Fila As Integer
Dim Final As Integer
Dim Lista As String

For Fila = 1 To ComboBox1.ListCount
ComboBox1.RemoveItem 0
Next Fila

For Fila = 2 To 10000
If Hoja2.Cells(Fila, 1) = "" Then
Final = Fila - 1
Exit For
End If
Next
For Fila = 2 To Final
Lista = Hoja2.Cells(Fila, 1)
ComboBox1.AddItem (Lista)
Next
End Sub
Private Sub CommandButton1_Click()
Dim Fila As Integer
Dim Final As Integer
Dim Existencia As Integer
Dim Total As Integer

For Fila = 2 To 10000
If Hoja3.Cells(Fila, 1) = "" Then
Final = Fila
Exit For
End If
Next

Hoja3.Cells(Final, 1) = Me.ComboBox1
Hoja3.Cells(Final, 2) = Me.txt_Nombre
Hoja3.Cells(Final, 3) = Me.txt_FechaIng
Hoja3.Cells(Final, 4) = Me.txt_Factura
Hoja3.Cells(Final, 5) = Me.txt_Proveedor
Hoja3.Cells(Final, 6) = Me.txt_Cantidad

For Fila = 2 To 10000
If Hoja5.Cells(Fila, 1) = Hoja3.Cells(Final, 1) Then
Existencia = Hoja5.Cells(Fila, 3)
Total = Me.txt_Cantidad + Existencia
Hoja5.Cells(Fila, 3) = Total
Exit For
End If
Next

Me.ComboBox1 = ""
Me.txt_Nombre = ""
Me.txt_FechaIng = ""
Me.txt_Factura = ""
Me.txt_Proveedor = ""
Me.txt_Cantidad = ""

End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub

1 respuesta

Respuesta
2

Estoy algo confundido, en tu código tienes tienes 2 veces este mismo evento

Private Sub ComboBox1_Change()

Entonces cuando lo ejecutas envía un error "Se ha detectado un nombre ambiguo: Combobox1.Change"

Mejor envíame tu archivo con el formulario, me dices qué columna es para el combo1, qué columna es para el combo2 y qué columnas son para los textbox.

Reviso el código y le hago los cambios.

¡Gracias! Gracias te lo envío al correo como me dijiste

Agregué esto a tu formulario

Private Sub ComboBox1_Change()
'Por.Dante Amor
    txt_Nombre = ""
    Set r = Hoja2.Columns("C")
    Set b = r.Find(ComboBoxPartida, lookat:=xlWhole)
    If Not b Is Nothing Then
        ncell = b.Address
        Do
            If Hoja2.Cells(b.Row, "A") = ComboBox1 Then
                txt_Nombre = Hoja2.Cells(b.Row, "B")
            End If
            Set b = r.FindNext(b)
        Loop While Not b Is Nothing And b.Address <> ncell
    End If
End Sub
Private Sub ComboBoxPartida_Change()
'Por.Dante Amor
    ComboBox1.Clear
    txt_Nombre = ""
    Set r = Hoja2.Columns("C")
    Set b = r.Find(ComboBoxPartida, lookat:=xlWhole)
    If Not b Is Nothing Then
        ncell = b.Address
        Do
            ComboBox1.AddItem Hoja2.Cells(b.Row, "A")
            Set b = r.FindNext(b)
        Loop While Not b Is Nothing And b.Address <> ncell
    End If
End Sub
Private Sub UserForm_Initialize()
'Por.Dante Amor
    For i = 2 To Hoja2.Range("C" & Rows.Count).End(xlUp).Row
        agregar ComboBoxPartida, Hoja2.Cells(i, "C")
    Next
End Sub
Sub agregar(combo As ComboBox, dato As String)
'Por.Dante Amor
    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

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas