Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-05
2
Medium Priority
?
1,945 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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

971 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