VBA Sort headers and subtotals

wilkisa

Well-known Member
Joined
Apr 7, 2002
Messages
657
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
I have created the following macro:

Sub SortSubMacroTest()
'
' SortSubMacroTest Macro
' Macro recorded 7/25/2007 by Shirlene
'
Range("K4").Select
Selection.EntireColumn.Delete
Range("A1:B1").Select

With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$4"
.PrintTitleColumns = ""
End With

ActiveSheet.PageSetup.PrintArea = ""

With ActiveSheet.PageSetup
.PrintHeadings = False
.PrintGridlines = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintErrors = xlPrintErrorsDisplayed
End With

Range("A4").Select

Call Subtotals

With Selection
.VerticalAlignment = xlBottom
.WrapText = True
End With

Range("A4").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Rows.AutoFit

Call PageBreak

Range("A1").Select

End Sub

Sub PageBreak()
Dim RNG As Range
Dim CurrNM As String

CurrNM = Range("A5").Value
For Each RNG In Range("A5", Range("A65536").End(xlUp))
If Not (CurrNM = RNG.Value Or RNG = "") Then
CurrNM = RNG.Value
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=RNG
End If
Next RNG
End Sub

Sub Subtotals()

Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Selection.Sort Key1:=Range("A5"), Order1:=xlAscending, Key2:=Range("B5") _
, Order2:=xlAscending, Key3:=Range("D5"), Order3:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
xlSortNormal
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(7, 8, 11) _
, Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End Sub


The Subtotal routine works just fine if I put it into its own module and F8 through it. However, when I call it from SortSubMacroTest it will sort my header row just as if it was a row of data. In the Sort part of the macro, I changed xlGuess to xlTrue and then it didn't sort the header row but then it would not do the subtotals. It gave me an error that it didn't know which row was the header row!

Help! Anyone?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
No response from anyone yet...has anyone got any ideas?
 
Upvote 0

Forum statistics

Threads
1,214,405
Messages
6,119,323
Members
448,887
Latest member
AirOliver

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