Macro muy especifico de suma y total

hola a todos para finalizar un proyecto tengo que hacer lo siguiente con todas las hojas que tienen el mismo formato QUE INCLUYE UN SUBTOTAL DEL TIPO CUENTA DE LAS COLUMNAS FAMILIA Y PNETO. EJEMPLO:

J K L

1 FAMILIA PNETO

2 NR 20

3 NR 20

4 NR 30

5 NR 40

6 CUENTA ANR 4

7 OND 35

8 CUENTA OND 1

9 ABR 20

10 ABR 30

11 CUENTA ABR 2

12 CUENTA GENERAL 7

13

14

LO QUE ESTA EN NEGRITAS ES EL RESULTADO DE LOS SUBTOTALES.

lo que se pretende es que aparezca la suma de pneto DE CADA FAMILIA en la casilla de la derecha de todas las casillas "cuentas" en la columna L, y debajo de la casilla de cuenta general aparezca un total de la suma de toda la columna L


J K L
1 FAMILIA PNETO
2 NR 20
3 NR 20
4 NR 30
5 NR 40
6 CUENTA ANR 4
110
7 OND 35
8 CUENTA OND 1 35
9 ABR 20
10 ABR 30
11 CUENTA ABR 2 50
12 CUENTA GENERAL 7
13
14 TOTAL 195


el problema es que todas las hojas pueden contener un numero indefinido de datos y familias, entonces

y cuando solo es un registro de familia no se como hacer la suma, mi propuesta es en ese caso seria sumarle 0, PERO NOS E SI EXISTA OTRA ALTERNATIVA

les agradezco su ayuda y espero su propuesta

muchas gracias

su amigo D.


1 Respuesta

Respuesta
1

Te mando mi solución. Posiciónate en la celda K1 y ejecuta esta macro:

Sub ejemplo()
'por luismondelo
Do While ActiveCell.Value <> ""
If Right(ActiveCell.Offset(0, -1), 7) = "general" Then
ActiveCell.Offset(0, 1).Value = suma
Exit Sub
End If
If Left(ActiveCell.Offset(0, -1), 2) = "Cu" Then
espacios = ActiveCell.Value
ActiveCell.Offset(0, 1).Value = Application.WorksheetFunction.Sum(Range(ActiveCell.Offset(-1, 0), ActiveCell.Offset(-espacios, 0)))
suma = suma + ActiveCell.Offset(0, 1).Value
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub

recuerda finalizar la consulta

muchas gracias por tu propuesta pero lo que pasa es que necesito que la macro se ejecute en todas las hojas y aparte que cree debajo de cuenta general una celda que diga TOTAL (en k) y a su derecha (en L) poner la suma total.

lo primero intente solucionarlo asi:

Sub ejemplo()
'por filas
For Each su In Sheets
su.Select
su = Range("k1").Select
Do While ActiveCell.Value <> ""
If Right(ActiveCell.Offset(0, -1), 7) = "general" Then
ActiveCell.Offset(0, 1).Value = suma
Exit Sub
End If
If Left(ActiveCell.Offset(0, -1), 2) = "Cu" Then
espacios = ActiveCell.Value
ActiveCell.Offset(0, 1).Value = Application.WorksheetFunction.Sum(Range(ActiveCell.Offset(-1, 0), ActiveCell.Offset(-espacios, 0)))
suma = suma + ActiveCell.Offset(0, 1).Value
End If
ActiveCell.Offset(1, 0).Select
Loop
Next su
End Sub

pero no puedo ejecutarlo debido a un error de compilación para ser exacto error 1004

ayuda por favor

Ok, pues te mando la solución para hacerlo en todas las hojas de tu libro

Sub ejemplo()
'por luismondelo
For Each hoja In ActiveWorkbook.Sheets
hoja.Select
Range("k1").Select
Do While ActiveCell.Value <> ""
If Right(ActiveCell.Offset(0, -1), 7) = "general" Then
ActiveCell.Offset(1, 1).Value = suma
ActiveCell.Offset(1, -1).Value = "SUMA TOTAL"
Exit Sub
End If
If Left(ActiveCell.Offset(0, -1), 2) = "Cu" Then
espacios = ActiveCell.Value
ActiveCell.Offset(0, 1).Value = Application.WorksheetFunction.Sum(Range(ActiveCell.Offset(-1, 0), ActiveCell.Offset(-espacios, 0)))
suma = suma + ActiveCell.Offset(0, 1).Value
End If
ActiveCell.Offset(1, 0).Select
Loop
Next
End Sub

recuerda finalizar la consulta

gracias a tu ayuda ya vi cual era el problema, debido a que tenia una hoja oculta ocurría un error con select del tipo worksheet y el código funciona bien pero a la hora de ejecutar el macro solo se queda en la primera hoja debido al Exit Sub después de suma total, si lo quito me genera errores en la suma,

y una pregunta mas, en la solución que me diste el next no debería llevar la variable hoja?

gracias por tus soluciones tan rápidas

Ok, la macro correcta quedaría así:

Sub ejemplo()
'por luismondelo
For Each hoja In ActiveWorkbook.Sheets
hoja.Select
Range("k1").Select
Do While ActiveCell.Value <> ""
If Right(ActiveCell.Offset(0, -1), 7) = "general" Then
ActiveCell.Offset(1, 1).Value = suma
ActiveCell.Offset(1, -1).Value = "SUMA TOTAL"
goto salto
End If
If Left(ActiveCell.Offset(0, -1), 2) = "Cu" Then
espacios = ActiveCell.Value
ActiveCell.Offset(0, 1).Value = Application.WorksheetFunction.Sum(Range(ActiveCell.Offset(-1, 0), ActiveCell.Offset(-espacios, 0)))
suma = suma + ActiveCell.Offset(0, 1).Value
End If
ActiveCell.Offset(1, 0).Select
Loop
salto:
Next
End Sub

en el next no es necesario poner la variable

Recuerda finalizar

ya vi cual era el problema lo que pasaba es que al momento de darle goto salto la variable de la suma total se quedaba almacenada y se sumaba al segundo total de la segunda hoja y así sucesivamente, lo que hice fue agregar un suma=0 después de salto:

asi me quedo a mi la solución:

Sub ejemplo()
'suma automática
Columns("L:L").Select
Selection.NumberFormat = "0"
For Each hoja In ActiveWorkbook.Sheets
Columns("L:L").Select
Selection.NumberFormat = "0"
hoja.Select
Range("k1").Select
Do While ActiveCell.Value <> ""
If Right(ActiveCell.Offset(0, -1), 7) = "general" Then
ActiveCell.Offset(1, 1).Value = suma
ActiveCell.Offset(1, -1).Value = "SUMA TOTAL"
GoTo salto
End If
If Left(ActiveCell.Offset(0, -1), 2) = "Cu" Then
espacios = ActiveCell.Value
ActiveCell.Offset(0, 1).Value = Application.WorksheetFunction.Sum(Range(ActiveCell.Offset(-1, 0), ActiveCell.Offset(-espacios, 0)))
suma = suma + ActiveCell.Offset(0, 1).Value
End If
ActiveCell.Offset(1, 0).Select
Loop
salto: suma = 0
Next
End Sub

asi ya la variable se vacía para la hoja 2 y asi hasta hoja x .y ya no tengo problemas muchas gracias, desde la Misteriosa tierra de México te mando un saludo

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas