how to macro to auto sub-total and grand total of manpower plan used in the project

david2005wang

New Member
Joined
Apr 8, 2022
Messages
13
Office Version
  1. 2021
  2. 2019
  3. 2013
Platform
  1. Windows
I have "manpower plan" sheet, and the No. lists in Column "A from 1 to N (example is from 1 to 3), Month in row 2 from 1 to M (example is from “E” to “P”).
Each time, I need to calculate the subtotal Manpower for each of Team (from 1 to N) including grant total for all teams by each month from 1 to M.
I plan to use VBA codes introduced by two buttons ("insert subtotal", and "grand total") to meet the following purpose.

1 When I click “insert subtotal” button after I put the mouse cursor in the position of active cell (the location of Sub-total) below last row of each team, it will insert one row, copy the format of above row and auto input “Sub-Total” words into the active cell, Change the background color of the row into grey, Draw a thick line under the sub-total row, and finally auto input the subtotal formular to calculate and fill the number of the sub-sum for each of month of each team.

2. when click the “grand total” button, will auto input the grand total formular to calculate and fill the number of the grand sum for each of month of all team.

I try some codes but didn't work well, so, could any expert help me with specific codes to meet the above targets.
Manpower Plan.xlsm
ABCDEFGHIJKLMNOPQRST
1Well Pads Construction Manpower PlanED+ Month
2123456789101112
3No.CategoryPlan to StartPlan to End
41Project Management Team
51.1Deputy Project ManagerED+2ED+1211111111111
61.2Construction ManagerED+10ED+12111
71.3Site ManagerED+6ED+121111111
8Sub-Total011112222333
92Geotechnical Team
102.1Team LeaderED+1ED+9111111111
112.2Geotechnical EngineerED+1ED+3222
122.3Topographic EngineerED+1ED+822222222
132.4Drilling machine operatorED+1ED+1144444444444
142.5LabourED+1ED+3121212
152.6AdministratorED+1ED+12222222222222
16Sub-Total232323999997662
173Camp Installation Team
183.1Site ManagerED+10ED+101
193.2Construction EngineerED+10ED+101
203.3QHSE EngineerED+10ED+101
21Sub-Total000000000300
22Grand Total232424101011111191295
Manpower Plan
Cell Formulas
RangeFormula
E21:P21,E8:P8E8=SUBTOTAL(9,E4:E7)
E16:P16E16=SUBTOTAL(9,E10:E15)
E22:P22E22=SUBTOTAL(9,E4:E21)
 

Attachments

  • Screenshot 2024-05-21 114947.png
    Screenshot 2024-05-21 114947.png
    34 KB · Views: 7

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I have writen simple codes to support the three buttons and make the function possible (insert subtotal, grandtotal, deletetotal) referring to attached sheet1. But what i want to say is put the formula Subtotal function into the cells of row of Sub-Total from column E to column P referring to the attached sheet2.. I also consider my code is too complex, and should be more easier code to meet the same functions. could any expert to advices?



Const ColLeft As Long = 5 'manpower plan start month
Const ColRight As Long = 16 'manpower plan finish month

Private Sub Subtotal_Click()

Dim Columa, Rowmaup, Rowmalow, R, C As Long
Dim ws As Worksheet
Columa = ActiveCell.Column
Debug.Print Columa
Rowmalow = ActiveCell.Row
Set ws = Worksheets("Manpower Plan")
If Rowmalow < 5 Or Columa <> 2 Then Exit Sub 'select only Column "B" and Row > 4.

'Find the sub-total range
For R = Rowmalow To 4 Step -1

If ws.Range("B" & R).Value = "Sub-Total" Then
Rowmaup = R + 2 'find the subtotal top row
Exit For
Else
Rowmaup = 5 'if no subtotal in column "B" means it is the first subtotal for top line, find the subtotal top row
End If

Next R

'Insert Sut-Total in the activeCell, and setup the color, fond and borders
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False

'ws.Range("B" & Rowmalow + 1).EntireRow.Insert
ws.Range("B" & Rowmalow + 1) = "Sub-Total"
ws.Range("B" & Rowmalow + 1).Font.Bold = True

ws.Range(Cells(Rowmalow + 1, 1), Cells(Rowmalow + 1, ColRight)).Interior.Color = RGB(211, 211, 211)

'Sub-Total Caculations
For C = ColLeft To ColRight
Cells(Rowmalow + 1, C) = Application.WorksheetFunction.Sum(Range(Cells(Rowmalow, C), Cells(Rowmaup, C)))
Next C

End Sub


Private Sub GrandTotal_Click()

Dim ws As Worksheet
Dim LR As Long
Dim i As Long
Dim Columa As Long

Set ws = Worksheets("Manpower Plan")
LR = ws.Range("B" & Rows.Count).End(xlUp).Row
ws.Range("B" & LR + 1).Value = "Grand Total"
Range(Cells(LR + 1, 1), Cells(LR + 1, ColRight)).Select

With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
End With

With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
End With

With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
End With

With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.Weight = xlThick

End With

With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.Weight = xlThick

End With


With Range(Cells(LR + 1, 1), Cells(LR + 1, ColRight))
'.WrapText = True
.Font.Bold = True
End With

For Columa = ColLeft To ColRight Step 1
Cells(LR + 1, Columa) = Application.WorksheetFunction.Sum(Range(Cells(4, Columa), Cells(LR, Columa))) / 2
Next Columa
End Sub


Private Sub DeleteRowTotal_Click()
Dim ws As Worksheet
Dim LR, R As Long
Set ws = Worksheets("Manpower Plan")

LR = ws.Range("B" & Rows.Count).End(xlUp).Row
For R = LR To 4 Step -1
If ws.Range("B" & R).Value = "Sub-Total" Or ws.Range("B" & R).Value = "Grand Total" Then Range("B" & R).EntireRow.Delete
Next R
End Sub

Attached Sheet1
Manpower Plan.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1Well Pads Construction Manpower PlanED+ Month
2123456789101112
3No.CategoryPlan to StartPlan to End
41Project Management Team
51.1Deputy Project ManagerED+2ED+1211111111111
61.1Deputy Project ManagerED+2ED+1211111111111
71.2Construction ManagerED+10ED+12111
81.3Site ManagerED+6ED+12011112222333
9Sub-Total033334444666
102Geotechnical Team
112.1Team LeaderED+1ED+9111111111
122.2Geotechnical EngineerED+1ED+3222
132.3Topographic EngineerED+1ED+822222222
142.4Drilling machine operatorED+1ED+1144444444444
152.5LabourED+1ED+3121212
162.6AdministratorED+1ED+12222222222222
17Sub-Total232323999997662
183Camp Installation Team
193.1Site ManagerED+10ED+101
203.2Construction EngineerED+10ED+101
213.3QHSE EngineerED+10ED+101
22Sub-Total000000000300
23Grand Total23252511111212121014117
24
25
Manpower Plan


Attached Sheet2
Manpower Plan.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1Well Pads Construction Manpower PlanED+ Month
2123456789101112
3No.CategoryPlan to StartPlan to End
41Project Management Team
51.1Deputy Project ManagerED+2ED+1211111111111
61.2Construction ManagerED+10ED+12111
71.3Site ManagerED+6ED+12011112222333
8Sub-Total022223333555
92Geotechnical Team
102.1Team LeaderED+1ED+9111111111
112.2Geotechnical EngineerED+1ED+3222
122.3Topographic EngineerED+1ED+822222222
132.4Drilling machine operatorED+1ED+1144444444444
142.5LabourED+1ED+3121212
152.6AdministratorED+1ED+12222222222222
16Sub-Total232323999997662
173Camp Installation Team
183.1Site ManagerED+10ED+101
193.2Construction EngineerED+10ED+101
203.3QHSE EngineerED+10ED+101
21Sub-Total000000000300
22Grand Total23252511111212121014117
23
24
Manpower Plan
Cell Formulas
RangeFormula
E21:P21,E8:P8E8=SUBTOTAL(9,E5:E7)
E16:P16E16=SUBTOTAL(9,E10:E15)
E22:P22E22=SUBTOTAL(9,E5:E21)
 

Attachments

  • Screenshot 2024-05-22 101709.png
    Screenshot 2024-05-22 101709.png
    34.8 KB · Views: 3
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,224,813
Messages
6,181,112
Members
453,021
Latest member
Justyna P

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