Auto Row insert if there is different number in the cell - Macro required

harinsh

Active Member
Joined
Feb 7, 2012
Messages
273
Hi All,

I hope can anyone help me with the below requirement.

I have data as below and need to insert one new row whenever number changes. This should happen through macro because my data would be very huge like 500 to 1000 rows so, sometimes it goes beyond this number.

Ex:-
Column 1
650
650
650
690
690
680
680

Out put Should be
Column 1
650
650
650

690
690

680
680

Kindly help me on this and provide your suggestions.

Thank you.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
harinsh,

Sample raw data:


Excel 2007
A
1650
2650
3650
4690
5690
6680
7680
8
9
10
Sheet1


After the macro:


Excel 2007
A
1650
2650
3650
4
5690
6690
7
8680
9680
10
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub InsertRowOnChange()
' hiker95, 0515/2013
' http://www.mrexcel.com/forum/excel-questions/702983-auto-row-insert-if-there-different-number-cell-macro-required.html
Dim r As Long, lr As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
  If Cells(r - 1, 1) <> Cells(r, 1) Then Rows(r).Insert
Next r
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the InsertRowOnChange macro.
 
Upvote 0
Thanks you very much for your inputs....I need small customization here and hope you can help me...

I want to insert new row if you find different value/text in the cell and also if we get empty cell then it should exclude that row and continue the task.

Input
Output
A1A1
12121212
1313
12121313
1212
1212
54541212
5454
66665454
5454
6666

<colgroup><col><col></colgroup><tbody>
</tbody>


Let me know if you need further info.
 
Upvote 0
harinsh,

You are very welcome. Glad I could help.

I want to insert new row if you find different value/text in the cell and also if we get empty cell then it should exclude that row and continue the task.

Sample raw data:


Excel 2007
A
11212
21313
31212
41212
5
65454
75454
86666
9
10
11
12
Sheet1


After the new macro:


Excel 2007
A
11212
2
31313
4
51212
61212
7
85454
95454
10
116666
12
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub InsertRowOnChange_V2()
' hiker95, 05/30/2013
' http://www.mrexcel.com/forum/excel-questions/702983-auto-row-insert-if-there-different-number-cell-macro-required.html
Dim r As Long, lr As Long
Application.ScreenUpdating = False
On Error Resume Next
Range("A1", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
  If Cells(r - 1, 1) <> Cells(r, 1) Then Rows(r).Insert
Next r
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the InsertRowOnChange_V2 macro.
 
Upvote 0
Thanks for your code...I want to ignore the row and not to delete because I have done same segregation for different parameter so, if I delete the row then those changes will go off...Please provide updated code....Thanks
 
Upvote 0
harinsh,

I want to ignore the row and not to delete because I have done same segregation for different parameter

You should have mentioned this parameter from the beginning.

Sample raw data:


Excel 2007
A
11212
21313
31212
41212
5
65454
75454
86666
9
10
11
12
Sheet1


After the new macro:


Excel 2007
A
11212
2
31313
4
51212
61212
7
85454
95454
10
116666
12
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub InsertRowOnChange_V3()
' hiker95, 05/31/2013
' http://www.mrexcel.com/forum/excel-questions/702983-auto-row-insert-if-there-different-number-cell-macro-required.html
Dim r As Long, lr As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
  If Cells(r, 1) = "" Or Cells(r - 1, 1) = "" Then
    'do nothing
  ElseIf Cells(r - 1, 1) <> Cells(r, 1) Then
    Rows(r).Insert
  End If
Next r
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the InsertRowOnChange_V3 macro.
 
Upvote 0
Hi Hiker95.

Your really genius and it is working exactly as per my requirement....

Thank you very much for your efforts....

Have a great day :)
 
Upvote 0
harinsh,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,980
Members
448,934
Latest member
audette89

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