Macro para reinicio de consecutivo dependiendo del cambio de mes

Te quiero pedir que ayudes con el siguiente caso:

Usando el siguiente código genero un consecutivo en la fila A, hasta el último valor encontrado en la tabla

 r = 1
    j = 10
    For i = 6 To h1.Range("A" & Rows.Count).End(xlUp).Row
        Set b = h2.Columns("B").Find(h1.Cells(i, "A"), lookat:=xlWhole)
        If Not b Is Nothing Then
            u2 = h3.Range("A" & Rows.Count).End(xlUp).Row + 1
            h3.Cells(u2, "A").Value = r
            r= r+1

Pero colocando en práctica el mismo me surge el siguiente requerimiento: El consecutivo se reinicie en 1 cada vez que empiece un nuevo mes según la fecha que se ubica en la columna B.

Como siempre cuento tu ayuda.

2 Respuestas

Respuesta
1

Este es el resultado de la macro

y esta es la macro

Sub numerar_por_meses()
meses = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
Set datos = Range("a1").CurrentRegion
With datos
    .Sort key1:=Range(.Columns(2).Address), order1:=xlAscending, Header:=True
    r = .Rows.Count
    Set datos = .Rows(2).Resize(r - 1)
    For i = 1 To r - 1
        .Cells(i, 3) = Month(.Cells(i, 2))
    Next i
    Set datos = .CurrentRegion
        For j = 1 To 12
        mes = meses(j)
        .Columns(3).Select
        cuenta = WorksheetFunction.CountIf(.Columns(3), mes)
        If cuenta = 0 Then GoTo salir
        fila = WorksheetFunction.Match(mes, .Columns(3), 0)
       Set ames = .Rows(fila).Resize(cuenta)
        With ames
            .Cells(1, 1) = 1
            If cuenta > 1 Then
            .Cells(1, 1).AutoFill Destination:=Range(.Columns(1).Address), Type:=xlFillSeries
            End If
        End With
    Next j
End With
Salir:
    Datos. Columns(3). ClearContents
End Sub
Respuesta
1

Te anexo la macro.

Sub Macro1()
    With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
        .Formula = "=IF(ISTEXT(R[-1]C[1]),1,IF(MONTH(R[-1]C[1])=MONTH(RC[1]),R[-1]C+1,1))"
        .Value = .Value
    End With
End Sub

[Sal u dos

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas