Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

730 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