Macro to insert a blank line after each subtotal

fmw5162022

New Member
Joined
May 16, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

Currently, I have a macro for sorting and subtotaling a list of customers and their daily total sales. Those work as expected, providing the results I desire.

I need to insert a blank row AFTER each subtotal, and am unable to accomplish this.
Below are my Sort and Subtotal macros:


VBA Code:
Sub SortSalesDetail()

' SortSalesDetail Macro
' Sort Sales Detail
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name Like "####-##-##_Tickets" Then
        ws.[A1].CurrentRegion.Sort key1:=ws.[C1], Header:=xlYes, Key2:=ws.[E1], Header:=xlYes
ws.Name = ws.Name & "_s"

    End If
Next

    ActiveWindow.SmallScroll Down:=-63
End Sub



VBA Code:
Sub Subtotal()
'
' Subtotal Macro
'

    Selection.Subtotal GroupBy:=3, Function:=xlSum, TotalList:=Array(9), _
        Replace:=True, PageBreaks:=False, SummaryBelowData:=True
 
End Sub


Thanking you very much in advance for your help!!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
your subtotal works on changes of the 3rd column, so if you insert an empty row, that's another value, so needs a new subtotal ... .
As workaround, you can make the row of the subtotal twice it normal height and the vertical alignement to the top.
Visual that 'll give the same effect. Is that a solution ?
 
Upvote 0
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. My signature block below has more details. I have added them for you this time. ?

See if this does what you want after the Subtotal code** has been run.
Test with a copy of your workbook.

VBA Code:
Sub Insert_Blank_Rows()
  Dim c As Range
  
  Application.ScreenUpdating = False
  For Each c In Columns("B").SpecialCells(xlBlanks)
    If Right(c.Offset(, 1).Value, 6) = " Total" Then c.Offset(1).EntireRow.Insert
  Next c
  Application.ScreenUpdating = True
End Sub

**
It is never a good idea to use a word that vba already has a special meaning for as a variable, procedure or module name. Therefore I would recommend renaming your 'Subtotal' procedure.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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