Glenn Stearns
asked on
Add Formulas in VBA to Sub-Total Rows
I have a sheet which I can sort and subtotal fine in VBA. Here is the code:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
Range("A1").Select
ActiveWorkbook.Worksheets( "December 12").Sort.SortFields.Clear
ActiveWorkbook.Worksheets( "December 12").Sort.SortFields.Add Key:=Range( _
"D2:D55"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets( "December 12").Sort
.SetRange Range("A1:H55")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.Subtotal GroupBy:=4, Function:=xlCount, TotalList:=Array(4), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End Sub
The code above puts the count of the number of rows in the sub-total section in column D of the sub-total rows in the sheet for each subtotal section. This is what I want it to do. Now, on each sub-total row, there are three other columns that I want the code to calculate a number and place it at the foot of the sub-total columns just as it does for the one sub-total the code is already calculating.
Now that we have sub-total rows in the worksheet, I need to add to the code so that VBA will look for each sub-total row and run a COUNTIF formula on columns E, F, and H. For columns E and F, it is to COUNTIF the criteria is "Y", and for column H, the criteria is "N". Then it needs to put the result of the COUNTIF formulas on the sub-total row for each section of sub-totals. The number of rows in any given sub-total section is dynamic and can change any time the code is run. I just do not know how to have VBA locate only the rows in each sub-total section, find the sub-total rows, and run the COUNTIF formula on the proper column with the proper criteria.
After it completes the COUNTIF process, I need VBA to set each sub-total row, columns A through H, to be highlighted in yellow and boldfaced.
A copy of how the sheet is to look is attached.
Any help you can provide on this will be appreciated!
Sample-Account-Spreadsheet.xlsx
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
Range("A1").Select
ActiveWorkbook.Worksheets(
ActiveWorkbook.Worksheets(
"D2:D55"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets(
.SetRange Range("A1:H55")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.Subtotal GroupBy:=4, Function:=xlCount, TotalList:=Array(4), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End Sub
The code above puts the count of the number of rows in the sub-total section in column D of the sub-total rows in the sheet for each subtotal section. This is what I want it to do. Now, on each sub-total row, there are three other columns that I want the code to calculate a number and place it at the foot of the sub-total columns just as it does for the one sub-total the code is already calculating.
Now that we have sub-total rows in the worksheet, I need to add to the code so that VBA will look for each sub-total row and run a COUNTIF formula on columns E, F, and H. For columns E and F, it is to COUNTIF the criteria is "Y", and for column H, the criteria is "N". Then it needs to put the result of the COUNTIF formulas on the sub-total row for each section of sub-totals. The number of rows in any given sub-total section is dynamic and can change any time the code is run. I just do not know how to have VBA locate only the rows in each sub-total section, find the sub-total rows, and run the COUNTIF formula on the proper column with the proper criteria.
After it completes the COUNTIF process, I need VBA to set each sub-total row, columns A through H, to be highlighted in yellow and boldfaced.
A copy of how the sheet is to look is attached.
Any help you can provide on this will be appreciated!
Sample-Account-Spreadsheet.xlsx
ASKER
Gowflow...
That works beautifully!
I did note that, in the Macro (1), I had used a predefined range of lines 2 through 55 because that is how many lines the sheet had on it. However, with each run of this, there will be a different number of lines on the spreadsheet - could be 25; could be 200. How do I change the code to look for and find all the active lines on the spreadsheet, no matter how many there are, before running the InsertCOUNTIF Sub? Getting past this issue will make the whole code work excellently in any situation. I should have mentioned this in my initial post, but failed to do so. I apologize!
Thanks again!
Glenn
That works beautifully!
I did note that, in the Macro (1), I had used a predefined range of lines 2 through 55 because that is how many lines the sheet had on it. However, with each run of this, there will be a different number of lines on the spreadsheet - could be 25; could be 200. How do I change the code to look for and find all the active lines on the spreadsheet, no matter how many there are, before running the InsertCOUNTIF Sub? Getting past this issue will make the whole code work excellently in any situation. I should have mentioned this in my initial post, but failed to do so. I apologize!
Thanks again!
Glenn
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so much! It runs beautifully!
You have 2 options you can run the routine created sepeartly after having produced your subtotal (or ran your vba subtotal) or you can add this line
InsertCOUNTIF
In your vba code to be like this
Open in new window
I have attached the workbook with a button starting the countif if you choose the option to do it manually.
I have not included your macro in the workbook attached as it is refering to sheet December12 that does not exist in the workbook.
If you have any question pls do not hesitate to ask.
Rgds/gowflow
Sample-Account-Spreadsheet.xlsm