Ordenar en forma ascendente en excel vba

Para Dante buenas tardes

Dante continuando con mi proyecto, deseo que se ordene por nombre cuando ejecuto el reporte

Intenté con este código que el falta ?

 u = h2.Range("A" & Rows.Count).End(xlUp).Row
    With h2.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("C6:C" & u), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

1 respuesta

Respuesta
2

Prueba así

    u = h2.Range("A" & Rows.Count).End(xlUp).Row
    With h2.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("C6:C" & u), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange h2.Range("A5:D" & u)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

NOTA: Si ordenas por alumno, entonces la numeración de la columna A se movería. Si quieres que tenga un consecutivo, tendrías que poner la numeración después de ordenar.

Faltó un h2, quedaría así:

    u = h2.Range("A" & Rows.Count).End(xlUp).Row
    With h2.Sort
        .SortFields.Clear
        .SortFields.Add Key:=h2.Range("C6:C" & u), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange h2.Range("A5:D" & u)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

H o l a: Experto Dante 

He adecuado todo el código a mi nuevo archivo de proyecto quedó estupendo, pero no consigo enumerar después de ordenar

Te anexo el código:

If Cboxaño.ListIndex = -1 Or Cbociclo_report = -1 Or Cboesp_report = -1 Then
            MsgBox "DEBE SELECIONAR TODOS LOS CRITERIOS:" + Chr(10) + Chr(10) + "* ESPECIALIDAD" + Chr(10) + "* CICLO" + Chr(10) + "* PERIODO", vbInformation, "IESPP MEO"
        Exit Sub
    End If
    'Por.Dante Amor
    Set h1 = Sheets("Pagosxciclo")
    Set h2 = Sheets("Rpt2x")
    h2.Range("A10:E" & h2.Range("E" & Rows.Count).End(xlUp).Row + 10).ClearContents
    j = 10
    n = 1
    '
    For i = 4 To h1.Range("A" & Rows.Count).End(xlUp).Row
        espec = IIf(Cboesp_report = "", h1.Cells(i, "D"), Cboesp_report)
        cicl = IIf(Cbociclo_report = "", h1.Cells(i, "E"), Cbociclo_report)
        peri = IIf(Cboxaño = "", h1.Cells(i, "K"), Cboxaño)
       ' If espec <> "" Then espec = Val(espec)
        '
        If h1.Cells(i, "D") = espec And h1.Cells(i, "E") = cicl And h1.Cells(i, "K") = peri Then
            h2.Cells(j, "A") = n
            h2.Cells(j, "B") = h1.Cells(i, "C")
            h2.Cells(j, "C") = CDate(h1.Cells(i, "A"))
            h2.Cells(j, "D") = h1.Cells(i, "G")
            h2.Cells(j, "E") = h1.Cells(i, "J")
           n = n + 1
           j = j + 1
        End If
    Next
         u = h2.Range("A" & Rows.Count).End(xlUp).Row
        With h2.Sort
            .SortFields.Clear
            .SortFields.Add Key:=h2.Range("B10:B" & u), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange h2.Range("A9:E" & u)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        'u1 = h2.Range("D" & Rows.Count).End(xlUp).Row
       'h2.Range("D" & u1 + 1) = "Total"
         u = h2.Range("E" & Rows.Count).End(xlUp).Row
        h2.Range("E" & u + 1) = WorksheetFunction.Sum(Range("E10:E" & u))
        Unload Me

Dante no consigo enumerar correlativamente antes de ordenar 

Solamente pon un contador después de ordenar.

Suponiendo que quieres el consecutivo en la columna A, en la columna B tienes la fecha, empezando en la fila 6

n = 1
for i = 6 to range("B" & rows.count).end(xlup).row
   cells(i, "A")  = n
   n = n +1
next

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas