Link to home
Start Free TrialLog in
Avatar of mas1963
mas1963

asked on

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.
See attachced report for more info

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)

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

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)
    .AdvancedFilter Action:=xlFilterInPlace, Unique:=True
    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)
 '   .AdvancedFilter Action:=xlFilterInPlace, Unique:=True
 '   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
Avatar of patrickab
patrickab
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of mas1963
mas1963

ASKER

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 .

Thanks again for your help.

Avatar of mas1963

ASKER

also length of Account Description  will be dynamic.
ASKER CERTIFIED SOLUTION
Avatar of patrickab
patrickab
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Try the sumif formula

like "=SUMIF(A1:A7,"=3",B1:B7)"
Avatar of mas1963

ASKER

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

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
Avatar of mas1963

ASKER

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
Avatar of mas1963

ASKER

with help of response I was able to achive the result