Como detectar items repetidos en un listbox al agregar datos

Gran foro

Tengo un pequeño formularia con

Listbox1 (5 columnas)

Textbox1

Commandbutton1

Label1

Label2

Al ingresar un numero en textbox y dar click en commandbutton1 el listbox agrega un nuevo item con el valor del texbox y me rellena las otras cuatro culumnas con los valores de un rango de una hoja

Mi pregunta es como poner un valor en textbox y si no existe ese valor en el listbox lo agrege y si existe que no lo agregue pero que sume el uno a lo que esta en ListBox1.List(I, 2)
Les dejo es código:

Private Sub CommandButton1_Click()
codigo = Me.TextBox1.Value

Set codigo = Sheets("PRODUCTOS").Range("A:A").Find(codigo, LookIn:=xlValues, lookat:=xlWhole)

cantidad = codigo.Offset(0, 7)
If Not codigo Is Nothing Then

If cantidad = "1" Then

Me.ListBox1.AddItem codigo
I = ListBox1.ListCount - 1
ListBox1.List(I, 1) = codigo.Offset(0, 1)

ListBox1.List(I, 2) = cantidad
Punitario = codigo.Offset(0, 2)
ListBox1.List(I, 3) = Application.WorksheetFunction.Text(Punitario, "$#,##0.00;-$#,##0.00")
Total = cantidad * Punitario
ListBox1.List(I, 4) = Application.WorksheetFunction.Text(Total, "$#,##0.00;-$#,##0.00")
Me.TextBox1 = Empty
Me.TextBox1.SetFocus
Me.Label1 = Application.WorksheetFunction.Text(CInt(Me.Label1) + CInt(cantidad), "#,##0")
Me.Label2 = Application.WorksheetFunction.Text(CDbl(Me.Label2) + CDbl(Total), "$#,##0.00;-$#,##0.00")
Cuenta = Me.ListBox1.ListCount

Else
MsgBox "Producto Desactivado"
Me.TextBox1 = Empty
Me.TextBox1.SetFocus
error:
cantidad = cantidad + 1

End If
End If

End Sub

Private Sub UserForm_Initialize()
Me.CommandButton1.TabIndex = 0
Me.TextBox1.SetFocus
Me.ListBox1.ColumnCount = 5
Me.ListBox1.ColumnWidths = "70 pt; 120 pt; 58 pt; 63 pt; 63 pt"
End Sub

Espero haberme explicado bien, si algún experto me pudiera ayudar

1 respuesta

Respuesta
1

 H  o l a:

Te regreso la macro actualizada, depuré algunas instrucciones que no son necesarias:

Private Sub CommandButton1_Click()
'Act.Por.Dante Amor
    existe = False
    Set codigo = Sheets("PRODUCTOS").Range("A:A").Find(TextBox1, LookIn:=xlValues, lookat:=xlWhole)
    If Not codigo Is Nothing Then
        cantidad = codigo.Offset(0, 7)
        If cantidad = "1" Then
            'Buscar codigo en listbox
            For j = 0 To ListBox1.ListCount - 1
                If codigo = ListBox1.List(j, 0) Then
                    existe = True
                    registro = j
                    Exit For
                End If
            Next
            '
            If existe Then
                ListBox1.List(registro, 2) = Val(ListBox1.List(registro, 2)) + 1
            Else
                ListBox1.AddItem codigo
                i = ListBox1.ListCount - 1
                ListBox1.List(i, 1) = codigo.Offset(0, 1)
                ListBox1.List(i, 2) = cantidad
                Punitario = codigo.Offset(0, 2)
                ListBox1.List(i, 3) = Format(Punitario, "$#,##0.00;-$#,##0.00")
                Total = cantidad * Punitario
                ListBox1.List(i, 4) = Format(Total, "$#,##0.00;-$#,##0.00")
                Label1 = Format(CInt(Label1) + CInt(cantidad), "#,##0")
                Label2 = Format(CDbl(Label2) + CDbl(Total), "$#,##0.00;-$#,##0.00")
            End If
        Else
            MsgBox "Producto Desactivado"
        End If
        TextBox1 = Empty
        TextBox1.SetFocus
    End If
End Sub

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

Re hola

Gracias por tu respuesta

Te comento

Aun me sigue agregando el item repetido, estoy tratando de revisar el código pero no doy, y creo que cambie dos numero del código

Set codigo = Sheets("PRODUCTOS").Range("A:A").Find(codigo, LookIn:=xlValues, lookat:=xlWhole)

cantidad = codigo.Offset(0, 5)
If Not codigo Is Nothing Then

ListBox1.List(I, 2) = cantidad
Punitario = codigo.Offset(0, 4)

Pero igual no creo que afecte

Igual seguiré revisando

Saludos

Seguramente tu código es numérico.

Ya le hice la adecuación a la macro:

Private Sub CommandButton1_Click()
'Act.Por.Dante Amor
    existe = False
    Set codigo = Sheets("PRODUCTOS").Range("A:A").Find(TextBox1, LookIn:=xlValues, lookat:=xlWhole)
    If Not codigo Is Nothing Then
        cantidad = codigo.Offset(0, 7)
        If cantidad = "1" Then
            'Buscar codigo en listbox
            For j = 0 To ListBox1.ListCount - 1
                If IsNumeric(codigo) Then
                    lista = Val(ListBox1.List(j, 0))
                Else
                    lista = ListBox1.List(j, 0)
                End If
                If codigo = lista Then
                    existe = True
                    registro = j
                    Exit For
                End If
            Next
            '
            If existe Then
                ListBox1.List(registro, 2) = Val(ListBox1.List(registro, 2)) + 1
            Else
                ListBox1.AddItem codigo
                i = ListBox1.ListCount - 1
                ListBox1.List(i, 1) = codigo.Offset(0, 1)
                ListBox1.List(i, 2) = cantidad
                Punitario = codigo.Offset(0, 2)
                ListBox1.List(i, 3) = Format(Punitario, "$#,##0.00;-$#,##0.00")
                Total = cantidad * Punitario
                ListBox1.List(i, 4) = Format(Total, "$#,##0.00;-$#,##0.00")
                Label1 = Format(CInt(Label1) + CInt(cantidad), "#,##0")
                Label2 = Format(CDbl(Label2) + CDbl(Total), "$#,##0.00;-$#,##0.00")
            End If
        Else
            MsgBox "Producto Desactivado"
        End If
        TextBox1 = Empty
        TextBox1.SetFocus
    End If
End Sub

Recuerda valorar la respuesta. S a l u d o  s

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas