Excel 2010 VBA to insert blank lines and add subtotals when applicable

In the attached Excel file in the tab titled "Current", I have some data that I rearranged in the tab titled "Desired".

Right now I am manually updating this based on the following criteria:

1. Insert two blank rows after any group of cells in column A that are merged.
2. Add a subtotal to the cells that are merged.
3. Insert one blank row after any cell in column A that is not merged.

Instead, it would be great if I could run a sub routine using VBA to accomplish this automatically.  Keep in mind, my data table always starts in row 5, but the number of rows could vary depending on the amount of zones have in the table.

I appreciate any suggestions.  This one goes way over my head.  Thanks in advance!
KP_SoCalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
pls upload a sample for our references. Alternatively you can do it by recording a macro, and see if it helps.
0
KP_SoCalAuthor Commented:
Sorry about that.  I forgot to hit the upload button.  The file has been attached.
Sample-File.xlsx
0
Martin LissOlder than dirtCommented:
It looks like your Sum formulas always sum only the last to values in column C for a set of data. For example in Alpha the formula is =SUM(C6:C7) rather than =SUM(C5:C7). Is that correct?
1
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Martin LissOlder than dirtCommented:
The attached workbook assumes you want =SUM(C6:C7). It contains a new sheet 'Template' that contains a formatted "Total" row which is copied to create the target sheet's Total row. You can hide the Template sheet if you like. The workbook also contains a 'Current (2)' sheet that I used for testing and is the result of the AddTotals macro that you'll find in Module1.
28706408.xlsm
1
KP_SoCalAuthor Commented:
Ooops, I meant for my sum function to include the entire data set.  So in the instanced you referenced, the correct range would have been C5:C7.

I really appreciate your help on this, but I couldn't get it to run properly after adding some merged cells below a single non-merged row.  If you run the attached spreadsheet, you'll see what I mean.

In summary, my main goal is to insert two blank rows directly below any merged cell or one blank row directly below a non-merged cell.  Any more ideas?
New-Version.xlsm
0
Martin LissOlder than dirtCommented:
I'm having a problem uploading a new workbook so here's the new code. It requires the 'Template' sheet. Is that a problem?

Sub AddTotals()
Dim lngRow As Long
Dim lngLastRow As Long
Dim lngStartRow As Long

Application.ScreenUpdating = False

With ActiveSheet
    lngLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    For lngRow = lngLastRow To 6 Step -1
        If .Cells(lngRow, 1).MergeCells Then
            ' Store the first row number of the merged cell
            lngStartRow = Cells(lngRow, 1).MergeArea.Row
            ' Select the row below the merged cell
            .Cells(lngRow + 1, 1).Select
           ' Insert a row above the selected row. In other words just
           ' below the merged cell.
            ActiveCell.EntireRow.Insert shift:=xlDown
            ' Copy a correctly formatted row to that new row
            Sheets("Template").Rows(1).Copy Destination:=ActiveCell.EntireRow
            ' Add formulas to the new row
            .Cells(ActiveCell.Row, "C").Formula = "=Sum(C" & lngStartRow & ":C" & ActiveCell.Row - 1 & ")"
            .Cells(ActiveCell.Row, "E").Formula = "=Sum(E" & lngStartRow & ":E" & ActiveCell.Row - 1 & ")"
            ' Remove the inherited formatting. Any blank cell can be copied and used as the source
            .Range("G1").Copy
            .Cells(ActiveCell.Row, "C").PasteSpecial xlPasteFormats
            .Cells(ActiveCell.Row, "E").PasteSpecial xlPasteFormats
            ' Select the row below the new row and insert another new row
            .Cells(lngRow + 2, 1).Select
            ActiveCell.EntireRow.Insert shift:=xlDown
            Sheets("Template").Rows(2).Copy Destination:=ActiveCell.EntireRow
            ' Set lngrow so the next time through the loop the unprocessed
            ' merged cell above it (if any) will be found
            lngRow = lngStartRow
        Else
            .Cells(lngRow + 1, 1).Select
            ActiveCell.EntireRow.Insert shift:=xlDown
            Sheets("Template").Rows(2).Copy Destination:=ActiveCell.EntireRow
        End If
    Next
End With

Application.ScreenUpdating = True
End Sub

Open in new window

1
Martin LissOlder than dirtCommented:
I was able to upload the workbook if you need it.
28706408a.xlsm
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KP_SoCalAuthor Commented:
I'm am so sorry for the delay response to this solution.  Your recommendation worked perfectly!  Thanks so much for your help on this and for the quick response. ;-)
0
Martin LissOlder than dirtCommented:
No problem. You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.