A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Hi there
EDITED:
Here is the link to your sample file with the VBA answer to your question
Heare are the macros
Sub AddSubTotals()
'Excel VBA to create subtotals.
''' by Jeovany CV @MS.Excel Forum_15-Jul-2023
Dim myData As Range
Dim i As Long
Dim voucher As String
Dim ton As Double
Dim subTotal As Double
Application.ScreenUpdating = False
Set myData = Range("A3", Range("H" & Rows.Count).End(xlUp)) ''' Data values only, Headers are excluded
myData.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo ''' Sort your data
i = 3
Do While Range("A" & i) <> ""
voucher = Range("C" & i).Value
ton = Range("E" & i).Value
If voucher = "SALES" Then ton = -ton
subTotal = subTotal + ton
If Range("A" & i) <> Range("A" & i + 1) Then
Rows(i + 1 & ":" & i + 2).EntireRow.Insert Shift:=xlDown
Range("D" & i + 1).Value = "TOTAL PENDING"
Range("E" & i + 1).Value = subTotal
Range("D" & i + 1).Resize(1, 2).Font.Bold = True
i = i + 2
subTotal = 0
End If
i = i + 1
Loop
Application.ScreenUpdating = True
End Sub
Sub Restore()
Dim bookNums As Range
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set bookNums = Range("A3", Range("A" & lastRow + 1))
bookNums.SpecialCells(4).EntireRow.Delete
End Sub
Regards
Jeovany