Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 1
Question How do I add a break after each subtotal?

Hello all,

I have a spreadsheet that I am trying to automatically subtotal and add a break before moving to the next group.

Example of how it is now:

test 1 10
test 1 10
test 1 10
test 2 20
test 2 20
test 2 20
test 3 30
test 3 30
test 3 30

Right now I can use the subtotal tool which will do this:

test 1 10
test 1 10
test 1 10
test 1 total 30
test 2 20
test 2 20
test 2 20
test 2 total 80
test 3 30
test 3 30
test 3 30
test 3 total 90

I need to know how to do a macro or format excel to make it look like this:

test 1 10
test 1 10
test 1 10
<b30</b

test 2 20
test 2 20
test 2 20
<b80</b

test 3 30
test 3 30
test 3 30
<b90</b


Please help me! thank you
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default How do I add a break after each subtotal?

Good Evening,

This might not be the most elegant way to accomplish what you're looking for, but the macro below works with your sample data. There are a few comments in the code to help you customize it. Essentially, the macro uses the Subtotal function to create subtotals, then goes through the newly-subtotaled data to change the way the total is displayed and insert a blank line. Optionally, you can remove the Grand Total and/or grouping.

Hope this helps.

Ben

Sub AddSubTotal()
Dim rValues As Range
Dim c As Range
Dim lRow(1 To 2) As Long
Dim strArray As String
Dim l As Long

'Delete next few lines of code if you wish to specify the range in code rather than by _
prompting the user.
On Error Resume Next
Set rValues = Application.InputBox("Please select a range", "Data Range", , , , , , 8)
On Error GoTo 0
If rValues Is Nothing Then
MsgBox ("Invalid Range")
Exit Sub
End If

'If not using inputbox method above, uncomment the next line
'Set rValues = Range("A1:B10")

lRow(1) = rValues.Rows.Count
rValues.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
lRow(2) = rValues.CurrentRegion.Rows.Count
If lRow(2) = lRow(1) Then Exit Sub 'user cancelled sub-total, so exit

Set rValues = rValues.Resize(lRow(2), 1)

For Each c In rValues
If c.Value = "Grand Total" Then
'Uncomment next 2 lines to clear Grand Total line
'c.ClearContents
'c.Offset(0, 1).ClearContents
ElseIf Right(c.Value, 5) = "Total" Then
l = l + 1
strArray = strArray & ", " & c.Address
c.Value = "<b" & c.Offset(0, 1).Value & "</b"
c.Offset(0, 1).Value = vbNullString
End If
Next c

strArray = Right(strArray, Len(strArray) - 2)
Set rValues = Range(strArray).Offset(1, 0)
rValues.EntireRow.Insert

'Uncomment next line if not using Grand Total
'Range("1:" & lRow(2) + l).Rows.Ungroup

'Uncomment next line to ungroup remaining rows
'Range("1:" & lRow(2) + l).Rows.Ungroup

Set rValues = Nothing

End Sub
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtotal To Include Item Description On Subtotal Line Tickfarmer Excel Discussion (Misc queries) 2 February 23rd 10 07:56 PM
Can I break "% in row" at each subtotal? Margie Excel Discussion (Misc queries) 0 December 8th 09 10:58 PM
Subtotal-new spreadsheet at each break? DRayner Excel Worksheet Functions 1 February 8th 08 12:35 AM
subtotal on page break shaqil Excel Programming 1 July 31st 07 05:54 PM
Subtotal page break error? Dips Excel Worksheet Functions 0 May 9th 06 11:21 PM


All times are GMT +1. The time now is 03:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"