# How subtotal in excel based on if condition.

I have a report and wants to subtotal based on column B (Account description) with the following criteria.
If Account Description is "Account Payable-Control" or "Amort - Leasing Commissions"  then subtotal and name the row as Accounting
esle if
If Account Description is "Impermis Cost Transfer " or "Impermis Income Transfer"  then subtotal and name the row as Transfer Cost
else if
If Account Description is "Leasing Comm - Accum Amort " or "Leasing Commissions"  then subtotal and name the row as Leasing
Else
Account Description other than above then subtotal all other and name the row as othere
in the end of data Grand total.

In report I already have a button name Calculate Amont that calculate the subtotal based on each category of Account Description. Calcualte Amont have following code for subtotal.

I was not able to attached the file as micro enable so to run the code copy the following code in excel as micro and run it.

Sub Button1_Click()
Dim rng As Range
Dim wks As Worksheet
Dim LastRow As Long
Dim LastRowMul As Long
Dim myRng As Range
Dim myRngMul As Range
Dim cell As Range
Set wks = ActiveSheet

Application.ScreenUpdating = False

With wks
.AutoFilterMode = False
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Columns(1).Insert
.Range("A9:a" & LastRow).Value = 1

'30 columns + one inserted
Set myRng = .Range("a2:a" & LastRow).Resize(, 31)

myRng.Subtotal groupby:=3, Function:=xlSum, totallist:=Array(4, 5), _
Replace:=True, pagebreaks:=False, _
summarybelowdata:=xlSummaryBelow

wks.UsedRange.ClearOutline

LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set myRng = .Range("a2:a" & LastRow)

'.Columns(1).Value = .Columns(1).Value

myRng.RemoveSubtotal

myRng.Resize(, 2).AutoFilter field:=1, Criteria1:="1"
myRng.Resize(, 2).AutoFilter field:=2, Criteria1:="*total"

On Error Resume Next
myRng.Offset(1, 0).Resize(myRng.Rows.Count - 1, 1) _
.Cells.SpecialCells(xlCellTypeVisible).EntireRow.Delete
On Error GoTo 0

.AutoFilterMode = False
.Columns(1).Delete
End With

With Intersect(wks.Columns("A"), wks.UsedRange)
Set rng = .SpecialCells(xlCellTypeVisible)
wks.ShowAllData
rng.EntireRow.Hidden = True
.SpecialCells(xlCellTypeVisible) = ""
rng.EntireRow.Hidden = False
End With

'With Intersect(wks.Columns("B"), wks.UsedRange)
'   Set rng = .SpecialCells(xlCellTypeVisible)
'   wks.ShowAllData
'   rng.EntireRow.Hidden = True
'   .SpecialCells(xlCellTypeVisible) = ""
'   rng.EntireRow.Hidden = False
'End With

'wks.UsedRange.AutoFormat Format:=xlRangeAutoFormatClassic1, Font:=True, Border:=True, Pattern:=True
Application.ScreenUpdating = True
End Sub

Subtotal.xlsx
###### Who is Participating?

Commented:
mas1963,

I have changed the formulae so that they include rows 2 to 10000 which should cover your needs - if not extend them in the formulae. They're in the attached file.

Patrick
Subtotals-02.xlsx
0

Commented:
mas1963,

Use a formula like this:

=SUMPRODUCT((\$B\$2:\$B\$39={"Accounts Payable - Control","Amort - Leasing Commissions"})*\$C\$2:\$C\$39)

It's in the attached file.

Patrick
Subtotals-01.xlsx
0

Author Commented:
Hi Partick,
Thanks for quick response.
I forgot to tell you that I am pulling the data from the SQL server baseed on Job id so Accounting, Transfer Cost, Leasing and Other sbutotal are dynamic i.e I could possible that other Job id may no have Account or leasing as Account description and how we can Grand total of  Accounting, Transfer Cost, Leasing and Other sbutotal .

0

Author Commented:
also length of Account Description  will be dynamic.
0

Commented:
Hi,

Try the sumif formula

like "=SUMIF(A1:A7,"=3",B1:B7)"
0

Author Commented:
Hi
I have a Button named Subtotal using the following formula for subtotal,
myRng.Subtotal groupby:=3, Function:=xlSum, totallist:=Array(4, 5), _
Replace:=True, pagebreaks:=False, _
summarybelowdata:=xlSummaryBelow

and after using the
Accounting=SUMPRODUCT((B2:B65535={"Accounts Payable - Control","Amort - Leasing Commissions"})*C2:C65535)
Transfer Cost=SUMPRODUCT((B2:B65535={"Impermis Cost Transfer","Impermis Income Transfer"})*C2:C65535)
Other==SUM(C2:C65535)-SUM(F2:F3)
Grand Total=SUM(F2:F4)

If I clicked the Sutotal button then the Grand Total also get added the subtotal value to Grand Total.
How I can I avoid to add the subtotal value to Grand Total.

Thanks for all help
0

Author Commented:
I used the following formula in Other
=SUMPRODUCT((RIGHT(B10:B65536,5)<>"Total")*1,D10:D65536)-SUM(I2:I5) and it work.
Is it a better way of doing above.

Thanks
Masood
0

Author Commented:
with help of response I was able to achive the result
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.