Auto Insert Row & Sum the Above Cells Excel Macro

Jyotirmaya

Board Regular
Joined
Dec 2, 2015
Messages
204
Office Version
  1. 2019
Platform
  1. Windows
In my Excel sheet I have values from Column A to Q. Column A is the Serial number Such as 1 2 3.....starting values from A6 as 1.
Column G is filled in without having Blank rows.
Example Column A6 is 1 & Again column A8 is 2.
Hence I want to insert a Row in 8th row & will have a Text TOTAL in G8 & it will Sum the values of H6+H7 & the Sum will be shown on H8 & similarly sum of I6+I7 & the sum will be shown on I8.


Column A data is filled in up to 5000 rows & Column G data also filled in upto 5000 rows. I want that if in the G column there are already "TOTAL" then it will just calculate the sum of H & I. & If there are no TOTAL then it will have the text TOTAL & will sum.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Could you please supply a sample of your data, both before & after?
Tools to help with that can be found here
https://www.mrexcel.com/forum/about-board/508133-attachments.html

SORRY FOR THE DELAY IN REPLY

BEFORE

BEFORE.JPG


AFTER
AFTER.JPG
 
Last edited:
Upvote 0
Both those images are virtually unreadable.
Could you please post the actual data using one of the tools, in the link I supplied
Thanks
 
Upvote 0
Both those images are virtually unreadable.
Could you please post the actual data using one of the tools, in the link I supplied
Thanks


BEFORE

Excel 2013/2016
ABCDEFGHI
1SL
2111020
332030
42555540
57767850
6444488860
77970
8895580
905590
1036630100
1165635110
126540120

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
BEFORE





AFTER


Excel 2013/2016
ABCDEFGHI
1
2
3
4
5SL
6111020
732030
8TOTAL3050
92555540
107767850
11444488860
127970
13895580
1405590
15TOTAL1140390
1636630100
1765635110
186540120
19TOTAL105330

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
AFTER

Worksheet Formulas
CellFormula
H8=SUM(H6:H7)
I8=SUM(I6:I7)
H15=SUM(H9:H14)
I15=SUM(I9:I14)
H19=SUM(H16:H18)
I19=SUM(I16:I18)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
How about
Code:
Sub InsertTotals()

   Dim Rng As Range
   
   With Range("A7:A" & Range("G" & Rows.Count).End(xlUp).Row)
      .SpecialCells(xlConstants).EntireRow.Insert
   End With
   
   With Range("G6", Range("G" & Rows.Count).End(xlUp))
      For Each Rng In .SpecialCells(xlConstants).Areas
         Rng.Offset(Rng.Count).Resize(1).Value = "Total"
         With Rng.Offset(Rng.Count, 1).Resize(1, 10)
            .Formula = "=sum(" & Rng.Offset(, 1).Address(False, False) & ")"
         End With
      Next Rng
   End With
End Sub
 
Upvote 0
How about
Code:
Sub InsertTotals()

   Dim Rng As Range
   
   With Range("A7:A" & Range("G" & Rows.Count).End(xlUp).Row)
      .SpecialCells(xlConstants).EntireRow.Insert
   End With
   
   With Range("G6", Range("G" & Rows.Count).End(xlUp))
      For Each Rng In .SpecialCells(xlConstants).Areas
         Rng.Offset(Rng.Count).Resize(1).Value = "Total"
         With Rng.Offset(Rng.Count, 1).Resize(1, 10)
            .Formula = "=sum(" & Rng.Offset(, 1).Address(False, False) & ")"
         End With
      Next Rng
   End With
End Sub


Thanks this is working but I want some changes, as I want only H & I column's sum hence I have changed
Code:
With Rng.Offset(Rng.Count, 1).Resize(1, 10)
into

With Rng.Offset(Rng.Count, 1).Resize(1, 2)

One more change I want that, if after a Serial there is already a TOTAL Text & SUM of H & I Column is there then No need to again Insert the TOTAL & no need to sum again, it shouldn't change, as with this code if there are already TOTAL it counts again & Make it Double Total of the Values.
 
Upvote 0
How about
Code:
Sub InsertTotals()

   Dim Rng As Range
   
   With Range("A7:A" & Range("G" & Rows.Count).End(xlUp).Row)
      .SpecialCells(xlConstants).EntireRow.Insert
   End With
   
   With Range("G6", Range("G" & Rows.Count).End(xlUp))
      For Each Rng In .SpecialCells(xlConstants).Areas
         If Rng.Offset(Rng.Count - 1).Resize(1).Value <> "Total" Then
            Rng.Offset(Rng.Count).Resize(1).Value = "Total"
            With Rng.Offset(Rng.Count, 1).Resize(1, 2)
               .Formula = "=sum(" & Rng.Offset(, 1).Address(False, False) & ")"
            End With
         Else
            Rng.Offset(Rng.Count).Resize(1).EntireRow.Delete
         End If
      Next Rng
   End With
End Sub
 
Upvote 0
How about
Code:
Sub InsertTotals()

   Dim Rng As Range
   
   With Range("A7:A" & Range("G" & Rows.Count).End(xlUp).Row)
      .SpecialCells(xlConstants).EntireRow.Insert
   End With
   
   With Range("G6", Range("G" & Rows.Count).End(xlUp))
      For Each Rng In .SpecialCells(xlConstants).Areas
         If Rng.Offset(Rng.Count - 1).Resize(1).Value <> "Total" Then
            Rng.Offset(Rng.Count).Resize(1).Value = "Total"
            With Rng.Offset(Rng.Count, 1).Resize(1, 2)
               .Formula = "=sum(" & Rng.Offset(, 1).Address(False, False) & ")"
            End With
         Else
            Rng.Offset(Rng.Count).Resize(1).EntireRow.Delete
         End If
      Next Rng
   End With
End Sub

Everything is OK with this formula, but I got an error that if there are already Total then It don't calculate the Sum of H & I column, for example The Total is in G9 then the sum of H6+H7+H8 in H9 & I6+I7+I8 in I9 should be shown, but already there is a Total & wrongly it is showing the sum of H6+H7 in H9 & I6+I7 in I9, in this case this formula don't calculate the Column value.
 
Upvote 0
Sorry but I don't understand what you're saying. If the word total is in G9 then the code I supplied doesn't do anything to the formulae in that row.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,519
Members
448,968
Latest member
Ajax40

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