Solved

How to insert a thick line after integer row in Excel.

Posted on 2014-07-29
8
182 Views
Last Modified: 2014-07-31
I have an excel document. I have a macro doing a ton of formatting. One of the formatting options organizes the data by names in Column A and then after each name change it adds three spaces.

I'm looking for an additional macro code to then go to columns k-y and for each new person, sum the columns. Each month the row totals will change.

Example Excel Attached. The rows in yellow are the rows i need to create via VBA script. Essentially some how get the script to read down the column selected (B) and when it sees a break in numbers, insert the formula. Then continue down. The number of rows will always be different although in this one its 2 then 1 then 3.
0
Comment
Question by:Brandon
  • 5
  • 3
8 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40227714
Attachment? :-)
0
 
LVL 3

Author Comment

by:Brandon
ID: 40227727
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40227872
The formulas in J, K, L, & M need to remain as they are, just copied down so that they apply on the total row.  The rest should be SUM() formulas.

I assume you want a thick border above the total row, but no other formatting, correct?

-Glenn
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40227940
Attached is a macro-enabled workbook with the following code that will add subtotals after each set of names (Add_Totals):
Sub Add_Totals()
    Dim rng As Range
    Dim cl As Object
    Dim r, c, x As Integer
    Dim strName As String
    Set rng = Range("A1:A" & Cells.SpecialCells(xlLastCell).Row)
    For Each cl In rng
        If (cl.Value = "") And (strName <> "") Then 'add total
            For c = 1 To 8
                cl.Offset(0, c).FormulaR1C1 = "=SUM(R[-" & x & "]C:R[-1]C)"
            Next c
            For c = 13 To 15
                cl.Offset(0, c).FormulaR1C1 = "=SUM(R[-" & x & "]C:R[-1]C)"
            Next c
            r = cl.Row
            cl.Offset(0, 9).Formula = "=IFERROR(I" & r & "/B" & r & ","""")"
            cl.Offset(0, 10).Formula = "=IFERROR((C" & r & "-E" & r & ")/C" & r & ","""")"
            cl.Offset(0, 11).Formula = "=IFERROR((D" & r & "-G" & r & ")/D" & r & ","""")"
            cl.Offset(0, 12).Formula = "=IFERROR(MIN(0.25,(C" & r & "-E" & r & "-F" & r & ")/C" & r & "),0.25)"
            strName = cl.Value 'should be blank
            x = 0
            With Range(cl.Offset(0, 1), cl.Offset(0, 15)).Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .ColorIndex = xlAutomatic
                .TintAndShade = 0
                .Weight = xlMedium
            End With
        Else
            strName = cl.Value
            If strName <> "" Then x = x + 1
        End If
    Next cl
End Sub

Open in new window


Note that I also changed the formulas in columns J through M to use the IFERROR function, which is significantly shorter:
J1: =IFERROR(I1/B1,"")
K1: =IFERROR((C1-E1)/C1,"")
L1: =IFERROR((D1-G1)/D1,"")
M1: =IFERROR(MIN(0.25,(C1-E1-F1)/C1),0.25)

You may want to change your existing macro that builds these formulas to use the IFERROR versions here.

Regards,
-Glenn
EE-Need-VBA.xlsm
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Author Comment

by:Brandon
ID: 40232524
Increased points since there will be at least one followup. Thanks for the changes, i'll implement.

This worked well with a couple of exceptions that are my fault.

the first three rows are header rows so this should start at A4. Actually it should start at G4 and provide the calculations for G-U and all the way down to the end.

Does this make sense?
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40232709
Okay...code and example file adjusted for change in layout.  Let me know if you have any further questions or issues.

Regards,
-Glenn
EE-Need-VBA.xlsm
0
 
LVL 3

Author Comment

by:Brandon
ID: 40232733
AMAZING!!
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40232870
I'm glad I could help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now