Mostrar solo una vez los datos

Para Dante Amor

¿Hola Dan como estas?

Recurro a su ayuda por favor!

Tengo el siguiente código que esta en un combobox "ComboBoxCodigo_Reb" al seleccionar un dato otro combobox "ComboBoxLote_Reb" muestra datos relacionados pero muestra repetidos y yo quiero que se muestre solo una vez cada dato relacionado acá va el código del combobox "ComboBoxCodigo_Reb"

Gracias

Saludos

Private Sub ComboBoxCodigo_Reb_Change()
 Application.ScreenUpdating = False
 On Error Resume Next
  'Me.ListBox2.Clear
    Dim myrange As Range, i As Integer, Celdi As Range, NameCeldi
    i = Sheets("Registros").Range("A" & Rows.Count).End(xlUp).Row
    Set myrange = Sheets("Registros").Range("A2:A" & i)
    ComboBoxLote_Reb.Clear
    Set Celdi = myrange.Find(What:=ComboBoxCodigo_Reb.Text)
    If Not Celdi Is Nothing Then
        NameCeldi = Celdi.Address
        Do
            If Cells(Celdi.Row, "G") > 0 Then
                With ComboBoxLote_Reb
                    .AddItem Sheets("Registros").Range("B" & Celdi.Row)
                    .Column(1, .ListCount - 1) = Celdi.Row
                End With
            End If
            Set Celdi = myrange.FindNext(Celdi)
       Loop While Not Celdi Is Nothing And Celdi.Address <> NameCeldi
    End If
    If ComboBoxLote_Reb.ListCount > 0 Then
        With ComboBoxLote_Reb
            .Visible = True
            .ListIndex = 0
        End With
    End If
    Application.ScreenUpdating = True
end sub

1 Respuesta

Respuesta
2

H o l a:

Te anexo la macro actualizada:

Private Sub ComboBoxCodigo_Reb_Change()
    Application.ScreenUpdating = False
    On Error Resume Next
    'Me.ListBox2.Clear
    Dim myrange As Range, i As Integer, Celdi As Range, NameCeldi
    i = Sheets("Registros").Range("A" & Rows.Count).End(xlUp).Row
    Set myrange = Sheets("Registros").Range("A2:A" & i)
    ComboBoxLote_Reb.Clear
    Set Celdi = myrange.Find(What:=ComboBoxCodigo_Reb.Text)
    If Not Celdi Is Nothing Then
        NameCeldi = Celdi.Address
        Do
            If Cells(Celdi.Row, "G") > 0 Then
                dato = Sheets("Registros").Range("B" & Celdi.Row)
                With ComboBoxLote_Reb
                    existe = False
                    For i = 0 To .ListCount - 1
                        Select Case StrComp(.List(i), dato, vbTextCompare)
                            Case 0
                                existe = True
                                Exit For 'ya existe en el combo y ya no lo agrega
                            Case 1
                                .AddItem dato, i
                                .Column(1, .ListCount - 1) = Celdi.Row
                                existe = True
                                Exit For 'Es menor, lo agrega antes del comparado
                        End Select
                    Next
                    If existe = False Then
                        .AddItem dato 'Es mayor lo agrega al final
                        .Column(1, .ListCount - 1) = Celdi.Row
                    End If
                End With
            End If
            Set Celdi = myrange.FindNext(Celdi)
       Loop While Not Celdi Is Nothing And Celdi.Address <> NameCeldi
    End If
    If ComboBoxLote_Reb.ListCount > 0 Then
        With ComboBoxLote_Reb
            .Visible = True
            .ListIndex = 0
        End With
    End If
    Application.ScreenUpdating = True
End Sub

NOTA: después de guardar el dato en el ComboBoxLote_Reb, estás agregando el número de fila: Celdi.Row, es decir, si tienes 2 registros iguales solamente almacenará al primero, y por ende, solamente te almacenará ese número de fila.


':)
'S aludos. D a n t e   A m o r . R ecuerda valorar la respuesta. G racias
':)

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas