Macro para agrupar y sumar registros con varios criterios
En la tabla de datos la columna E, G, I, J, L y M corresponde a ID únicos y en las columnas N y P hay dos criterios que se deben asociar a cada registro de los ID, para luego sumar los valores de las columnas Q, R, S, T, U y V según la combinación de los IDs con los criterios N y P .Cada ID tiene asignado un valor, es decir el ID EP (Columna E) va con el valor de EP (columna Q), cada registro tiene un centro de costos y grupo.

Sub sumarSS()
'Por Dante Amor
Set h11 = Sheets("REGSS")
Set h14 = Sheets("Hoja1")
h14.Cells.ClearContents
h11.[B9:AB9].Copy h14.[B9]
h14.[A9] = "REG"
r = 1
For i = 10 To h11.Range("E" & Rows.Count).End(xlUp).Row
Set s = h14.Columns("E").Find(h11.Cells(i, "E").Value, lookat:=xlWhole)
If Not s Is Nothing Then
h14.Cells(s.Row, "Q").Value = h14.Cells(s.Row, "Q").Value + h11.Cells(i, "Q").Value
Else
u1 = h14.Range("A" & Rows.Count).End(xlUp).Row + 1
h14.Cells(u1, "A").Value = r
h14.Cells(u1, "E").Value = h11.Cells(i, "E").Value
h14.Cells(u1, "F").Value = h11.Cells(i, "F").Value
h14.Cells(u1, "Q").Value = h11.Cells(i, "Q").Value
r = r + 1
End If
Next
For h = 10 To h11.Range("G" & Rows.Count).End(xlUp).Row
Set p = h14.Columns("G").Find(h11.Cells(h, "G").Value, lookat:=xlWhole)
If Not p Is Nothing Then
h14.Cells(p.Row, "R").Value = h14.Cells(p.Row, "R").Value + h11.Cells(h, "R").Value
Else
u2 = h14.Range("A" & Rows.Count).End(xlUp).Row + 1
h14.Cells(u2, "A").Value = r
h14.Cells(u2, "G").Value = h11.Cells(h, "G").Value
h14.Cells(u2, "H").Value = h11.Cells(h, "H").Value
h14.Cells(u2, "R").Value = h11.Cells(h, "R").Value
r = r + 1
End If
Next
Next
End Sub
'Ajuste el código de una pregunta anterior, y funciona bien,
'solo que esta vez tiene que tener en cuenta los criterios mencionados.
1 Respuesta
Respuesta de Dante Amor
2