+ Reply to Thread
Results 1 to 4 of 4

need excel macro, to add two blank rows after a value change, then autosum column C

Hybrid View

  1. #1
    Registered User
    Join Date
    04-30-2015
    Location
    USA
    MS-Off Ver
    EXCEL 2007
    Posts
    1

    need excel macro, to add two blank rows after a value change, then autosum column C

    so the column is:

    1
    1
    1
    2
    2
    2

    I want it to do this:
    1
    1
    1
    autosum column C

    2
    2
    2



    I found a similar one but it does it for column E. I need it for column C and I need to add two rows after every value change.

    Sub sbttls()
    Dim aArea As Range, i As Integer
    i = 0
    For Each aArea In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants).Areas
    i = i + 1
    If i <> 1 Then
    With Cells(aArea.Row + aArea.Rows.Count, 5)
    .Value = WorksheetFunction.Sum(Range(Cells(aArea.Row, 5), Cells(aArea.Row + aArea.Rows.Count - 1, 5)))
    With .Font
    .ColorIndex = 5
    .Bold = True
    End With
    End With
    End If
    Next aArea
    End Sub

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: need excel macro, to add two blank rows after a value change, then autosum column C

    Hello and welcome to the forum,

    Try this code :
    Sub ttls()
        Dim aArea As Range, i As Long, LastRow As Long
        i = 0
    
        '/Add blank rows
        LastRow = Cells(Rows.Count, 3).End(xlUp).Row
        For i = LastRow To 2 Step -1
            If (Cells(i, 3) <> Cells(i - 1, 3)) Then
                If Cells(i, 3) <> "" And Cells(i - 1, 3) <> "" Then
                    Rows(i).EntireRow.Insert
                    Rows(i).EntireRow.Insert
                End If
            End If
        Next i
    
        'Add Sub Total
        i = 0
        For Each aArea In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants).Areas
            i = i + 1
            If i <> 1 Then
                With Cells(aArea.Row + aArea.Rows.Count, 3)
                    .Value = WorksheetFunction.Sum(Range(Cells(aArea.Row, 3), Cells(aArea.Row + aArea.Rows.Count - 1, 3)))
                    With .Font
                        .ColorIndex = 5
                        .Bold = True
                    End With
                End With
            End If
        Next aArea
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,088

    Re: need excel macro, to add two blank rows after a value change, then autosum column C

    Another way
    If you don't want the first block to have the SUM, remove the "'" at the beginning of
            'I = I + 1
            'If I <> 1 Then
            'End If



    Sub sbttls2()
    Dim aArea As Range
    Dim LR  As Long, I As Long
    Const WkCol As String = "C"
    
        LR = Range(WkCol & Rows.Count).End(xlUp).Row
        For I = LR To 2 Step -1
            If (Cells(I, WkCol) <> Cells(I - 1, WkCol)) Then
                    Cells(I, WkCol).Resize(2, 1).Insert Shift:=xlDown
            End If
        Next I
        
        LR = Range(WkCol & Rows.Count).End(xlUp).Row
        For Each aArea In Range(Cells(1, WkCol), Cells(LR, WkCol)).SpecialCells(xlCellTypeConstants).Areas
            'I = I + 1
            'If I <> 1 Then
                With Cells(aArea.Row + aArea.Rows.Count, WkCol)
                    .Formula = "=SUM(R[-" & aArea.Rows.Count & "]C:R[-1]C)"
                    With .Font
                    .ColorIndex = 5
                    .Bold = True
                    End With
                End With
            'End If
        Next aArea
    End Sub
    - Battle without fear gives no glory - Just try

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: need excel macro, to add two blank rows after a value change, then autosum column C

    One more:

    Sub dubb()
    Dim i As Long
    For i = Range("C" & Rows.Count).End(3).Row To 2 Step -1
        If Cells(i, "C") <> Cells(i + 1, "C") Then
            Rows(i + 1).Resize(2).Insert
        End If
    Next i
    For Each numrange In Columns(3).SpecialCells(xlConstants, xlNumbers).Areas
        sumaddr = numrange.Address(False, False)
        numrange.Offset(numrange.Count, 0).Resize(1, 1).Formula = "=SUM(" & sumaddr & ")"
        c = numrange.Count
    Next numrange
    nodata:
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 04-22-2014, 02:12 PM
  2. Replies: 8
    Last Post: 12-09-2013, 09:05 PM
  3. Macro to delete blank rows if column I is blank for the row
    By tiger10012 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-10-2013, 03:01 PM
  4. Macros to insert 2 blank row and autosum a column after every 200 rows in data range.
    By sachinsingh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-22-2013, 09:55 AM
  5. Change AutoSum Direction in Excel
    By e@london in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 08-15-2012, 06:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1