Forum Discussion
Sorts, page breaks and subtotals, oh my...
Hello to anyone reading this! I'm having difficulty writing a macro and was hoping one of you would be generous enough to help me out.
What I'm trying to do:
I am trying to import data to a templated excel sheet and execute a macro that will sort by two columns (A = Item description, H = Tag type), Page break after a change in column H and if there is no change (not all item descriptions have a tag type entry) a break after Column A, and finally a sub total for every break on column H and a Total for every break on column A (using sums from Columns B and O). If possible I'd like to auto adjust for column width and wrap. Please not that the ranges are dynamic, however the column headers will remain in the same order.
Any help is appreciated!!!
What I have so far:
Sub SubTotals()
ActiveSheet.Copy After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = "Totals"
Worksheets("Totals").Activate
Dim arow As Long
Dim hrow As Long
arow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
hrow = Cells.Find(What:="*", _
After:=Range("H1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Range("A2").Select
ActiveWorkbook.Worksheets("Totals").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Totals").Sort.SortFields.Add Key:=Range("A2:A" & CStr(arow)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Totals").Sort.SortFields.Add Key:=Range("H2:H" & CStr(hrow)) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Totals").Sort
.SetRange Range("A1:O" & CStr(arow))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.Subtotal GroupBy:=8, Function:=xlSum, TotalList:=Array(2, 15), _
Replace:=False, PageBreaks:=True, SummaryBelowData:=True
Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2, 15), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
End Sub
This is not functioning as intended as it seems to not total B,O on the page break for column A changes. Ex. column A has the values Plant 1, Plant 2, Plant 3. Column H has values A,B,(Blank). The blanks in H seem to gum up the works and cause the pages to no longer break on changes in A. There are bound to be other issues, I know almost nothing about VBA.
Example file included. (no macros included in file)
1 Reply
- Strider051Copper Contributor
I could for sure use a hand on this still if anyone has the inclination I would appreciate it!