Solved

Format underline a range

Posted on 2016-08-11
2
89 Views
Last Modified: 2016-08-12
This is related to solved question https://www.experts-exchange.com/questions/28962735/Make-sub-to-format-and-total-ranges.html

For convenience, I'm attaching the same .xlsx files.

Add bottom borders under 2 total rows
I'd like to add bottom borders on the last 2 rows of each group as shown. I tried to change the code here but it just underlines the first cell of the 2nd row. I'd like to underline that whole row and the cells directly above that.

Add bottom borders under 2 total rows WRONG
Sub AddGroupAndTotalsAndUnderlines()
'
' Run after SeparateGroups
' Put BM's name on bolded line, and add bolded totals for each group.
' Also underline totals. It doesn't work correctly. Just 1 cell is underlined.
'
'
Dim rng As Range
Application.ScreenUpdating = False
For Each rng In Range("A:A").SpecialCells(xlCellTypeConstants, 2).Areas
    If rng.Cells(1).Row <> 1 Then
        rng.Cells(1).Copy rng.Cells(1).Offset(-1, 2)
        With rng.Cells(1).Offset(-1, 2).Font
            .Bold = True
            .Size = 14
            .Name = "Calibri"
        End With
        rng.Cells(rng.Rows.Count + 1).Offset(0, 5).Resize(, 8).FormulaR1C1 = "=SUM(R[-" & rng.Rows.Count & "]C:R[-1]C)"
        rng.Cells(rng.Rows.Count + 1).Offset(0, 5).Resize(, 8).Font.Bold = True
        With rng.Cells(rng.Rows.Count + 1).Offset(0, 5).Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
    End If
Next rng
Application.ScreenUpdating = True
End Sub

Open in new window


For my edification, I'd appreciate also an explanation of my error and how you fixed it. 8-)
Before.xlsx
After.xlsx
0
Comment
Question by:NVIT
[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 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41753344
Change this line:
With rng.Cells(rng.Rows.Count + 1).Offset(0, 5).Borders(xlEdgeBottom)

Open in new window

to this:
With rng.Cells(rng.Rows.Count + 1).Offset(0, 5).Resize(, 8).Borders(xlEdgeBottom)

Open in new window

0
 
LVL 30

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41753376
Please try this.....

Sub AddGroupAndTotalsAndUnderlines()
Dim rng As Range
Application.ScreenUpdating = False
For Each rng In Range("A:A").SpecialCells(xlCellTypeConstants, 2).Areas
    If rng.Cells(1).Row <> 1 Then
        rng.Cells(1).Copy rng.Cells(1).Offset(-1, 2)
        With rng.Cells(1).Offset(-1, 2).Font
            .Bold = True
            .Size = 14
            .Name = "Calibri"
        End With
        rng.Cells(rng.Rows.Count + 1).Offset(0, 5).Resize(, 8).FormulaR1C1 = "=SUM(R[-" & rng.Rows.Count & "]C:R[-1]C)"
        rng.Cells(rng.Rows.Count + 1).Offset(0, 5).Resize(, 8).Font.Bold = True
        With rng.Cells(rng.Rows.Count + 1).Offset(0, 5).Resize(, 8).Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        With rng.Cells(rng.Rows.Count + 1).Offset(0, 5).Resize(, 8).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
    
    End If
Next rng
Application.ScreenUpdating = True
End Sub

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

740 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