Total a column and repeat headings

marcidee

Board Regular
Joined
May 23, 2016
Messages
184
Office Version
  1. 2019
Please can someone help me with a script - I have a sheet that has a blank row every time a new name appears in column A

What i would like is to:

Place a total of the figures in column G - total in column F (last row)
Repeat the headings from row 1 on each blank line

Example of end result below

Thank you for your help


NameTimesheetP/E DateCompanyTime workedPay RateTotal Pay
0
Abimbola Dunsin (Dunsin) B1Thu 08 Aug 2019Peter Howes0.759.006.75
Abimbola Dunsin (Dunsin) B1Fri 09 Aug 2019Peter Howes0.59.004.5
Abimbola Dunsin (Dunsin) B1Sat 10 Aug 2019Phillip Mercer0.259.002.2513.5
NameTimesheetP/E DateCompanyTime workedPay RateTotal Pay
Afaque Solangi1Mon 12 Aug 2019Mohsen Taheri79.0063
Afaque Solangi1Tue 13 Aug 2019Mohsen Taheri79.0063
Afaque Solangi1Wed 14 Aug 2019Mohsen Taheri79.0063189
NameTimesheetP/E DateCompanyTime workedPay RateTotal Pay
Amalia Gatou B1Thu 15 Aug 2019Amanda King19.009
Amalia Gatou B1Fri 16 Aug 2019Amanda King0.259.002.25
Amalia Gatou B1Sat 17 Aug 2019Amanda King0.259.002.2513.5

<tbody>
</tbody>

I had posted this question on another forum however I did not find a solution - the link to this question is below:

https://www.ozgrid.com/forum/forum/h...es-in-column-a

The first part of the link is irrelevant as I have been fortunate to be given the solution in this forum by -'fluff' however I am looking for a solution to this question as I require both methods
 
Last edited by a moderator:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG13Aug32
[COLOR="Navy"]Dim[/COLOR] R [COLOR="Navy"]As[/COLOR] Range, lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
lst = Range("A" & Rows.Count).End(xlUp).Row
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] Range("G:G").SpecialCells(xlCellTypeConstants).Areas
    R(R.Count).Offset(, 1) = Application.Sum(R)
    [COLOR="Navy"]If[/COLOR] R(R.Count).Offset(, 1).Row < lst [COLOR="Navy"]Then[/COLOR]
        Range("A1:G1").Copy R(R.Count).Offset(1, -6)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] R
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
I am now having a problem to get the last part of the solution that you gave me last night working (total in column F and repeating first line) this were working fine - the sheet I was working on has changed slightly - but the only difference I can see is that column D contains no data (I left it like this so Column G remained as G. When running the script it is now not adding the headings or the totals?

I now see the below after running the script

NameP/E DateClientTime WorkedPay RateTotal Pay0
NameP/E DateClientTime WorkedPay RateTotal Pay
Abimbola Dunsin (Dunsin) BFri 02 Aug 2019Constance Rubidge0.759.006.75
Abimbola Dunsin (Dunsin) BSat 03 Aug 2019Constance Rubidge0.259.002.25
Abimbola Dunsin (Dunsin) BSat 10 Aug 2019Sheila Rice0.759.006.75
Abimbola Dunsin (Dunsin) BSun 11 Aug 2019Theresa Darling0.259.002.25
Afaque SolangiMon 12 Aug 2019Mohsen Taheri79.0063.00
Afaque SolangiTue 13 Aug 2019Mohsen Taheri79.0063.00
Afaque SolangiWed 14 Aug 2019Mohsen Taheri79.0063.00
Amalia Gatou BThu 15 Aug 2019Amanda King19.009.00
Amalia Gatou BFri 16 Aug 2019Amanda King0.259.002.25
Amalia Gatou BSat 17 Aug 2019Amanda King0.259.002.25

<colgroup><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
The code works based on the Blank rows , which is what you had in the first instance.
When you run the code the Header in row 1 are added to each blank row and also the totals for each set of data
Once you have run the code, those blank rows are filled with the header and running the code again will not do anything.
I noticed on your previous data there was a "0" in cell "G2", this "0" stopped the otherwise blank row 2 from also filling with the headers.
In the current data, Row 2 have now got headers in it and "H1" seems to have a "0" I think that is because after originally running the code, you removed the previous "0" in "G2" and ran the code again, resulting in what you have now.

NB:- Data Column "D" data should make no difference to the code.
To Run the code again to get a result, you will need to remove the headers Rows where they where previous filled, when the code was first run, and then run the code again.
Good Luck
Regrds Mick
 
Upvote 0
The sheet looks like the below before I am running the script and this is the script I am using

Option Explicit
Sub RunAlladdblank()
Call AddBlankRows
Call MG13Aug32

End Sub






Sub AddBlankRows()
'
Dim iRow As Integer, iCol As Integer
Dim oRng As Range


Set oRng = Range("a1")


iRow = oRng.Row
iCol = oRng.Column


Do
'
If Cells(iRow + 1, iCol) <> Cells(iRow, iCol) Then
Cells(iRow + 1, iCol).EntireRow.Insert Shift:=xlDown
iRow = iRow + 2
Else
iRow = iRow + 1
End If
'
Loop While Not Cells(iRow, iCol).Text = ""
'
End Sub






Sub MG13Aug32()
Dim R As Range, lst As Long
lst = Range("A" & Rows.Count).End(xlUp).Row
For Each R In Range("G:G").SpecialCells(xlCellTypeConstants).Areas
R(R.Count).Offset(, 1) = Application.Sum(R)
If R(R.Count).Offset(, 1).Row < lst Then
Range("A1:G1").Copy R(R.Count).Offset(1, -6)
End If
Next R
End Sub



NameTimesheetP/E DateClientTime WorkedPay RateTotal Pay
Abimbola Dunsin (Dunsin) B1Fri 02 Aug 2019Constance Rubidge0.759.006.75
Abimbola Dunsin (Dunsin) B1Fri 02 Aug 2019Constance Rubidge0.259.002.25
Abimbola Dunsin (Dunsin) B1Fri 02 Aug 2019Isobella Clark,0.59.004.50
Afaque Solangi1Mon 29 Jul 2019Mohsen Taheri79.0063.00
Afaque Solangi1Wed 31 Jul 2019Mohsen Taheri79.0063.00
Afaque Solangi1Fri 02 Aug 2019Mohsen Taheri79.0063.00
Amalia Gatou B1Wed 31 Jul 2019Amanda King19.009.00
Amalia Gatou B1Wed 31 Jul 2019Amanda King0.259.002.25
Ana Sofia Alves De Almieda (Sofia) Team Leader1Mon 29 Jul 2019Beryl Jospehine0.759.006.75
Ana Sofia Alves De Almieda (Sofia) Team Leader1Mon 29 Jul 2019Beryl Jospehine0.59.004.50
Ana Sofia Alves De Almieda (Sofia) Team Leader1Mon 29 Jul 2019Isobella Clark,0.59.004.50


<colgroup><col span="2"><col span="2"><col span="3"></colgroup><tbody>
</tbody>
 
Upvote 0
That seems to work OK for me except it places a Extra header in rows 2.
if you change :- iRow = 2 That should stop that!!
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG14Aug44
[COLOR="Navy"]Dim[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] R [COLOR="Navy"]As[/COLOR] Range
Lst = Range("A" & Rows.Count).End(xlUp).Row

[COLOR="Navy"]For[/COLOR] n = Lst To 3 [COLOR="Navy"]Step[/COLOR] -1
    [COLOR="Navy"]If[/COLOR] Cells(n, 1) <> Cells(n - 1, 1) [COLOR="Navy"]Then[/COLOR]
        Cells(n, 1).EntireRow.Insert shift:=xlDown
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n

Lst = Range("A" & Rows.Count).End(xlUp).Row
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] Range("G:G").SpecialCells(xlCellTypeConstants).Areas
        R(R.Count).Offset(, 1) = Application.Sum(R)
        [COLOR="Navy"]If[/COLOR] R(R.Count).Offset(, 1).Row < Lst [COLOR="Navy"]Then[/COLOR]
            Range("A1:G1").Copy R(R.Count).Offset(1, -6)
        [COLOR="Navy"]End[/COLOR] If
  [COLOR="Navy"]Next[/COLOR] R
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,128
Members
448,947
Latest member
test111

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