A B C D
PR-DATA-DESCRIZIONE-IMPORTO
è necessario verificare ed eventualmente eliminare riporti parziali già
creati:
Sub Riporti()
Dim Riga(1000) As Integer
Dim i As Integer
Dim k As Integer
Dim TotRip As String
Dim tb As Worksheet
Application.ScreenUpdating = False
'elimina vecchi riporti e/o righe di calcolo
Set tb = Worksheets(1)
TotRighe =
Application.WorksheetFunction.CountA(Worksheets(1).Range("D:D"))
For k = 2 To TotRighe
If IsEmpty(tb.Cells(k, 1)) Then tb.Rows(k).EntireRow.Delete
Next k
Rows(2).Insert
Rows(2).Select
Selection.Font.Bold = False
Cells(2, 3) = "Riporto Iniziale"
Cells(2, 3).Font.Bold = True
Cells(2, 4) = 0
Cells(2, 4).NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 "
Cells(2, 4).Font.Bold = True
Application.Goto Reference:=Worksheets(1).Cells(65536, 1).End(xlUp),
Scroll:=True
' necessario x calcolare N. pagine
NPag = Worksheets(1).HPageBreaks.Count 'conta esistenza N. pagine -1
' MsgBox "Numero di pagine: " & NPag + 1
If NPag = 0 Then GoTo Fine
For i = 1 To CInt(NPag)
Riga(i) = Worksheets(1).HPageBreaks(i).Location.Row
Next i
Riga(0) = 0
For i = 1 To NPag
Cells(Riga(i), 4).Select
Selection.EntireRow.Insert
TotRip = "=ROUND(sum(D" & CStr(Riga(i - 1)) & ":D" &
CStr(Riga(i) - 1) & "),2)"
Cells(Riga(i), 4) = TotRip
Cells(Riga(i), 4).Font.Bold = True
Cells(Riga(i), 3) = "Riporto"
Cells(Riga(i), 3).Font.Bold = True
Next i
'ricorregge prima formula
PrimaForm = Worksheets(1).HPageBreaks(1).Location.Row
Cells(PrimaForm, 4) = "=ROUND(sum(D1:D" & CStr(PrimaForm - 1) & "),2)"
'ultima formula
Riga_z = Cells(65536, 4).End(xlUp).Row
Riga_a = Cells(Riga_z, 2).End(xlUp).Row
Cells(65536, 4).End(xlUp).Offset(1, 0) = "=ROUND(sum(D" & CStr(Riga_a -
1) & ":D" & CStr(Riga_z) & "),2)"
Cells(65536, 4).End(xlUp).Offset(1, 0).Font.Bold = True
Cells(65536, 3).End(xlUp).Offset(1, 0) = "Totale"
Cells(65536, 3).End(xlUp).Offset(1, 0).Font.Bold = True
Application.Goto Reference:=Worksheets(1).Cells(2, 1), Scroll:=True
Exit Sub
Fine:
Riga_z = Cells(65536, 4).End(xlUp).Row
Cells(65536, 4).End(xlUp).Offset(1, 0) = "=ROUND(sum(D2:D" &
CStr(Riga_z) & "),2)"
Cells(65536, 4).End(xlUp).Offset(1, 0).Font.Bold = True
Cells(65536, 3).End(xlUp).Offset(1, 0) = "Totale"
Cells(65536, 3).End(xlUp).Offset(1, 0).Font.Bold = True
Application.Goto Reference:=Worksheets(1).Cells(2, 1), Scroll:=True
End Sub
Sub EliminaRiporti()
Dim k As Integer
Dim TotRighe
Dim tb As Worksheet
Application.ScreenUpdating = False
Set tb = Worksheets(1)
TotRighe =
Application.WorksheetFunction.CountA(Worksheets(1).Range("D:D"))
For k = 2 To TotRighe
If IsEmpty(tb.Cells(k, 1)) Then tb.Rows(k).EntireRow.Delete
Next k
Application.Goto Reference:=Worksheets(1).Cells(2, 1), Scroll:=True
End Sub |