Link to home
Start Free TrialLog in
Avatar of Brandon
BrandonFlag for United States of America

asked on

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

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.
Avatar of Glenn Ray
Glenn Ray
Flag of United States of America image

Attachment? :-)
Avatar of Brandon

ASKER

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
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
Avatar of Brandon

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Glenn Ray
Glenn Ray
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brandon

ASKER

AMAZING!!
I'm glad I could help.