Solved

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

Posted on 2013-01-05
2
1,888 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

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.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

632 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