Urgent pls: Insert new row and sum in blank rows

luvbite38

Active Member
Joined
Jun 25, 2008
Messages
368
Can some one kindly help me out.

I have an existing summary (see table 1) which I wish to summarise as shown in Table 2.

- I want a macro to automatically insert a row as soon the value is changed in column A (item) and than sum the total quantity of all items per category. Furthermore it should further sum the 8 (fixed types of each catergory - such as Apple will always have 8 sub catergories or Banana or any other item will always have 8 subs).

And in case if a user clicks to compile the data, I want macro to delete all newly added rows and repeat the entire process as explained above.

I hope it makes sense?

I dont know how to attach a sample file otherwise I'd have done that already........ please please please help.

************* Table 1 (existing summary page)**********
item total
apple1 1
apple2 2
apple3 2
apple5 2
apple8 9
b1 4
b8 6
grapes4 9
grapes6 11
grapes6 12
grapes6 10
grapes8 1


Kind Regards,

L
**************** Table 2 (Desired Summary Page)


apple1 1
apple2 2
apple3 2
apple5 2
apple8 9

APPLE 1 = 1 APPLE2 = 2 APPLE3 = 2 APPLE4 = 0 APPLE5 = 2 APPLE6 = 0 APPLE 7 = 0 APPLE8 = 9

b1 4
b8 6
b 1 = 4 b2 =0 b3 = 0 b4 = 0 b5 = 0 b6 = 0 b7 = 0 b8 = 6

grapes4 9
grapes6 11
grapes6 12
grapes6 10
grapes8 1
grapes 1 = 0 grapes2 = 0 grapes3 = 0 grapes4 = 9 grapes5 = 0 grapes6 = 33 grapes 7 = 0 grapes8 = 1
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Excel Workbook
AB
1itemtotal
2apple11
3apple22
4apple32
5apple52
6apple89
7b14
8b86
9grapes49
10grapes611
11grapes612
12grapes610
13grapes81
BEFORE
Excel Workbook
AB
1itemtotal
2apple11
3apple22
4apple32
5apple52
6apple89
7
8Total16
9
10b14
11b86
12
13Total10
14
15grapes49
16grapes611
17grapes612
18grapes610
19grapes81
20
21Total43
AFTER


Code:
Option Explicit

Sub AddSubtotals()
Dim Rw As Long, FR As Long

Rw = 2  'starting row
FR = Rw
Do
    If LettersOnly(Range("A" & Rw)) <> LettersOnly(Range("A" & Rw + 1)) Then
        Rows(Rw + 1).Resize(3).EntireRow.Insert xlShiftDown
        Range("A" & Rw + 2) = "Total"
        Range("B" & Rw + 2).FormulaR1C1 = "=SUM(R" & FR & "C:R" & Rw & "C)"
        Rw = Rw + 4
        FR = Rw
    Else
        Rw = Rw + 1
    End If
Loop Until Range("A" & Rw) = ""

End Sub


Function LettersOnly(txt As String) As String
    With CreateObject("VBScript.RegExp")
        .Pattern = "[^A-Z\s]+"      'leaves only letters and spaces
        .Global = True
        .IgnoreCase = True
        LettersOnly = Application.WorksheetFunction.Trim(.Replace(txt, ""))
    End With
End Function
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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