Macro to insert 2 empty rows and row of heading after subtotal row

New_To_Macros

New Member
Joined
Aug 1, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I would like to create a macro, which will insert
  • a subtotal after each change in either column B (Employment Status) or C (Type of Leave),
  • bold the entire subtotal row,
  • change the background of the subtotal row to grey,
  • insert 3 rows after the subtotal and
  • insert a line of headers before the beginning of the next set of data
  • sort the data by largest to smallest in column F ($ OF LEAVE), then from A-Z in column A (Employee Name)
The data is not always the same, as sometimes some employees leave the firm and new employees join the firm.

Any assistance on how to create this macro is greatly appreciated. Thank you!

Original Table

Employee NameEmployment StatusType of LeaveRate/HrHours of Leave$ of Leave
Andrew AndersonFull TimeVacation40.0010.00400.00
Birgit BraunFull TimeVacation30.0015.00450.00
Charly CampbellPart TimeVacation20.0010.00200.00
Donald DennisFull TimeSick40.0020.00800.00
Edward EgoStudentLieu Time15.005.0075.00
Fred FlintFull TimeLieu Time30.0015.00450.00
George GutenbergPart TimeLieu Time20.0010.00200.00
Henry HasePart TimeVacation20.0015.00300.00
Ida IgloStudentVacation15.0015.00225.00


What the table should look like after running the macro:

Image.PNG
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hello,

this gives the same results as your example.

VBA Code:
Sub subtotal()
'SORT DATA
    Range("A14:F" & Range("F" & Rows.Count).End(xlUp).Row).Select
    Selection.Sort Key1:=Range("B14"), Order1:=xlAscending, Key2:=Range("C14" _
        ), Order2:=xlDescending, Key3:=Range("F14"), Order3:=xlDescending, _
        Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:= _
        xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
        DataOption3:=xlSortNormal
'INSERT BLANK ROWS
    For MY_ROWS = Range("A" & Rows.Count).End(xlUp).Row To 15 Step -1
        If Range("B" & MY_ROWS).Value <> Range("B" & MY_ROWS - 1).Value Or _
            Range("C" & MY_ROWS).Value <> Range("C" & MY_ROWS - 1).Value Then
            For MY_ADD = 1 To 3
                Rows(MY_ROWS).Insert
            Next MY_ADD
        End If
    Next MY_ROWS
'COPY HEADER ROW
    For MY_ROWS = 14 To Range("A" & Rows.Count).End(xlUp).Row
        If IsEmpty(Range("A" & MY_ROWS).Value) And Not IsEmpty(Range("A" & MY_ROWS + 1).Value) _
            Then Rows(13).Copy Range("A" & MY_ROWS)
    Next MY_ROWS
'ADD SUBTOTAL TEXT
    For MY_ROWS = 14 To Range("A" & Rows.Count).End(xlUp).Row + 1
            If IsEmpty(Range("A" & MY_ROWS).Value) And IsEmpty(Range("A" & MY_ROWS + 1).Value) Then
            Range("A" & MY_ROWS).Value = "Subtotal of " & Range("B" & MY_ROWS - 1).Value & " " & _
                Range("C" & MY_ROWS - 1).Value
            Range("A" & MY_ROWS & ":F" & MY_ROWS).Interior.ColorIndex = 15
            Range("E" & MY_ROWS).Formula = "=SUM(E" & MY_ROWS - 1 & ":E" & Range("E" & MY_ROWS - 1).End(xlUp).Offset(1, 0).Row & ")"
            Range("F" & MY_ROWS).Formula = "=SUM(F" & MY_ROWS - 1 & ":F" & Range("F" & MY_ROWS - 1).End(xlUp).Offset(1, 0).Row & ")"
        End If
    Next MY_ROWS
End Sub

the code could be streamlined, but i'm off out soon.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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