Solved

VBA code to insert a blank row after Subtotal row in excel 2003

Posted on 2013-01-05
2
1,843 Views
Last Modified: 2013-01-05
Here is part of my excel data: (the actual excel file has hundreds of rows )

CityID      Quantity            
1                  3
1                  4
1                  4
Subtotal:  11
2                  3
2                  5
Subtotal:   8
Total:         19


I want to insert a blank row after each Subtotal row, format subtotal row in bold and size 11,  total number is single underlined.
Last row Total row is bold , size 12, and number is double underlined.
the result is as below.

How do I write VBA code to do it? thanks,
Result.jpg
0
Comment
Question by:HemlockPrinters
  • 2
2 Comments
 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
ID: 38747925
Hi, HemlockPrinters.

Edit: Minor change - ScreenUpdating turned off.

Please see attached. The code is...
Option Explicit

Sub Format_Totals()
Dim xCell As Range

Application.ScreenUpdating = False
    
    For Each xCell In Range("A1:A" & Range("A1").SpecialCells(xlLastCell).Row)
        If xCell = "Subtotal:" Then
            xCell.Offset(0, 1).Font.Underline = xlUnderlineStyleSingle
            With xCell.Resize(1, 2).Font
                .FontStyle = "Bold"
                .Size = 11
            End With
            xCell.Offset(1, 0).EntireRow.Insert
        ElseIf xCell = "Total:" Then
            With xCell.Resize(1, 2).Font
                .FontStyle = "Bold"
                .Size = 12
            End With
            xCell.Offset(0, 1).Font.Underline = xlUnderlineStyleDouble
        End If
    Next

Application.ScreenUpdating = True

End Sub

Open in new window

Regards,
Brian. Subtotal.xls
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38747968
Thanks, HemlockPrinters.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question