Insert Rows Where Column Value Changes

Neild137

New Member
Joined
Mar 23, 2017
Messages
48
Before you flag this, please note that I need to amend Existing code to be able to fit multiple cases. I have:

Code:
Sub InsertRowAtChangeInValue() 
    Dim lRow As Long 
    For lRow = Cells(Cells.Rows.Count, "AN").End(xlUp).Row To 2 Step -1 
        If Cells(lRow, "AN") <> Cells(lRow - 1, "AN") Then Rows(lRow).EntireRow.Insert 
    Next lRow 
End Sub

I need to amend this to add the following functionality:

1. Rows need to be added in worksheet titled "Location File Data", with the date reference column as the qualifier being "AN".
2. Be able to let me specify what the previous value to look for and next value changed to should be. Insert rows where this change happens. IE, there are rows of data for 2017, 2016, 2015, 2014 and 2013 respectively. I want the code to let me specify whether to look for the spot where 2017 data ends and 2016 begins, or where 2015 ends and 2014 begins, etc... I would rather this not be by prompt, but rather by a reference to a cell in a sheet titled "Compare" which contains the number of rows to insert
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Neild137,

AN will have dates that you wish to reference by year ? AN will be sorted ascending or descending?
 
Upvote 0
Yes, correct. AN has dates, like 3/31/17, 12/31/16, 12/31/15 and these are in descending order, starting with 2017, ending with 2013 at the bottom
 
Upvote 0
See if this helps.

Compare

*ABC
1Insert AboveRowsAt Row
2201724

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:101px;"><col style="width:64px;"><col style="width:75px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
C2{=IFERROR(MATCH(A2,YEAR('Location File Data'!AN1:AN1002),0),"No Match")}

<tbody>
</tbody>
Formula Array:
Produce enclosing
{ } by entering
formula with CTRL+SHIFT+ENTER!

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
Code:
Sub InsertRowAtChangeInValue()
    Dim myRng As Range
    Dim atRow, numRows As Long
    
    If Sheets("Compare").Range("C2") = "No Match" Or Sheets("Compare").Range("B2") <= 0 Then Exit Sub
    
    atRow = Sheets("Compare").Range("C2")
    numRows = Sheets("Compare").Range("B2") - 1
    
    Set myRng = Sheets("Location File Data").Range("AN" & atRow & ":AN" & atRow + numRows)
    
    myRng.EntireRow.Insert
    
End Sub

You enter Year to insert above in A2 Number of rows in B2
Array Formula in C2 computes the start row.

You need to edit AN range depth to suit.
Formula can be tweaked if you prefer to enter a year to insert below.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,591
Members
449,174
Latest member
chandan4057

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top