VBA Script for inserting new row and subtotal

fali34533

New Member
Joined
Apr 5, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi I I am trying to apply a filter and sort the data by department.
for each department I want to add a new line to separate them and in that new row I want a subtotal of column labelled as "total" .

I've got the following in VBA so far and need help in inserting a subtotal in the new row for column total for each department.

Sub Insert_Rows_Sort_Department()
' Apply filter to Row 1
ActiveSheet.Range("A1").AutoFilter
' Sort data by "Department"
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add2 Key:=Range("B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range("A:B")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Insert a row to separate each department
Dim lastRow As Long
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Long
For i = lastRow To 2 Step -1
If ActiveSheet.Cells(i, 2).Value <> ActiveSheet.Cells(i - 1, 2).Value Then
ActiveSheet.Rows(i).Insert Shift:=xlDown
End If
Next i


Any help will be much appriciated.

Thank you
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi I I am trying to apply a filter and sort the data by department.
for each department I want to add a new line to separate them and in that new row I want a subtotal of column labelled as "total" .

I've got the following in VBA so far and need help in inserting a subtotal in the new row for column total for each department.

Sub Insert_Rows_Sort_Department()
' Apply filter to Row 1
ActiveSheet.Range("A1").AutoFilter
' Sort data by "Department"
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add2 Key:=Range("B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range("A:B")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Insert a row to separate each department
Dim lastRow As Long
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Long
For i = lastRow To 2 Step -1
If ActiveSheet.Cells(i, 2).Value <> ActiveSheet.Cells(i - 1, 2).Value Then
ActiveSheet.Rows(i).Insert Shift:=xlDown
End If
Next i


Any help will be much appriciated.

Thank you

I want the out put to look like this
Name Department Total
Test1 Food 1
Test2 Food 1
Subtotal 2
Test4 Bills 4
Test4 Bills 1
Subtotal 5
 
Upvote 0

Forum statistics

Threads
1,215,593
Messages
6,125,715
Members
449,254
Latest member
Eva146

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top