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:=xlSummar yBelow
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(xlCell TypeVisibl e).EntireR ow.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(xlCellTypeVi sible)
wks.ShowAllData
rng.EntireRow.Hidden = True
.SpecialCells(xlCellTypeVi sible) = ""
rng.EntireRow.Hidden = False
End With
'With Intersect(wks.Columns("B") , wks.UsedRange)
' .AdvancedFilter Action:=xlFilterInPlace, Unique:=True
' Set rng = .SpecialCells(xlCellTypeVi sible)
' wks.ShowAllData
' rng.EntireRow.Hidden = True
' .SpecialCells(xlCellTypeVi sible) = ""
' rng.EntireRow.Hidden = False
'End With
'wks.UsedRange.AutoFormat Format:=xlRangeAutoFormatC lassic1, Font:=True, Border:=True, Pattern:=True
Application.ScreenUpdating = True
End Sub
Subtotal.xlsx
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
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:=xlSummar
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
.Cells.SpecialCells(xlCell
On Error GoTo 0
.AutoFilterMode = False
.Columns(1).Delete
End With
With Intersect(wks.Columns("A")
.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rng = .SpecialCells(xlCellTypeVi
wks.ShowAllData
rng.EntireRow.Hidden = True
.SpecialCells(xlCellTypeVi
rng.EntireRow.Hidden = False
End With
'With Intersect(wks.Columns("B")
' .AdvancedFilter Action:=xlFilterInPlace, Unique:=True
' Set rng = .SpecialCells(xlCellTypeVi
' wks.ShowAllData
' rng.EntireRow.Hidden = True
' .SpecialCells(xlCellTypeVi
' rng.EntireRow.Hidden = False
'End With
'wks.UsedRange.AutoFormat Format:=xlRangeAutoFormatC
Application.ScreenUpdating
End Sub
Subtotal.xlsx
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.
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.
ASKER
also length of Account Description will be dynamic.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Try the sumif formula
like "=SUMIF(A1:A7,"=3",B1:B7)"
Try the sumif formula
like "=SUMIF(A1:A7,"=3",B1:B7)"
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:=xlSummar yBelow
Application.DisplayAlerts = True
and after using the
Accounting=SUMPRODUCT((B2: B65535={"A ccounts Payable - Control","Amort - Leasing Commissions"})*C2:C65535)
Transfer Cost=SUMPRODUCT((B2:B65535 ={"Impermi s 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
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:=xlSummar
Application.DisplayAlerts = True
and after using the
Accounting=SUMPRODUCT((B2:
Transfer Cost=SUMPRODUCT((B2:B65535
Other==SUM(C2:C65535)-SUM(
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
ASKER
I used the following formula in Other
=SUMPRODUCT((RIGHT(B10:B65 536,5)<>"T otal")*1,D 10:D65536) -SUM(I2:I5 ) and it work.
Is it a better way of doing above.
Thanks
Masood
=SUMPRODUCT((RIGHT(B10:B65
Is it a better way of doing above.
Thanks
Masood
ASKER
with help of response I was able to achive the result
Use a formula like this:
=SUMPRODUCT(($B$2:$B$39={"
It's in the attached file.
Patrick
Subtotals-01.xlsx