Ordenar datos insertados por formulario por fecha reciente

Este código me pasaste (esta bien)

Private Sub CommandButton1_Click()
'Por.Dante Amor
    Set h = Sheets("altas")
    'primera fila vacía de la columna A
    u = h.Range("A" & Rows.Count).End(xlUp).Row + 1
    If u = 5001 Then
        MsgBox "Se alcanzó el límite de 5000"
        Exit Sub
    End If
    '
    h.Cells(u, "A") = TextBox1
    h.Cells(u, "B") = TextBox2
    h.Cells(u, "C") = TextBox3
    '
    'cambia la X por la columna que tiene la fecha
    With h.Sort
        .Sort.SortFields.Clear
        .SortFields.Add Key:=h.Range("X2:X" & u), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        'Cambia la A y la Z por el rango de columnas que vas a ordenar
        .SetRange h.Range("A1:Z" & u)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

pero quisiera que se ordenara por fecha reciente (el textbox3 debe tener formato fecha ya lo hice)

Entonces debe empezar a insertarse lo datos a partir de G6 porque en G5 dice : FECHA DE INGRESO.

Se inserta la fecha con su dato y se ordena todo de A ala G teniendo la fecha más reciente

1 respuesta

Respuesta
2

Te anexo la macro actualizada

Private Sub CommandButton1_Click()
'Por.Dante Amor
    Application.ScreenUpdating = False
    Set h = Sheets("altas")
    u = h.Range("G" & Rows.Count).End(xlUp).Row + 1
    If u = 5006 Then
        MsgBox "Se alcanzó el límite de 5000"
        Exit Sub
    End If
    '
    h.Cells(u, "A") = TextBox1
    h.Cells(u, "B") = TextBox2
    h.Cells(u, "G") = CDate(TextBox3)
    '
    With h.Sort
        .SortFields.Clear
        .SortFields.Add Key:=h.Range("G6:G" & u), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .SetRange h.Range("A5:Z" & u)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Application.ScreenUpdating = True
End Sub

sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas