How To Auto Insert Rows Based On Criteria?

NewbieFromNJ

New Member
Joined
May 12, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi guys,

Is there a way I can auto insert rows based on the information that is in column B? See attached image. I would like to insert two rows where the item number changes. Example, insert two rows separating 2470 and 2471 and so forth.

Thank you
 

Attachments

  • EXCEL QUESTION.png
    EXCEL QUESTION.png
    12 KB · Views: 14

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Welcome to the Board!

In order for VBA code to run automatically, there has to be some "event" that triggers it to run, like the opening of a file, the selection of a particular cell, or the update of a particular cell.
What would be the "event" to automatically trigger the VBA to run in this case?

The other option is to run the VBA code on command.
 
Upvote 0
Welcome to the Board!

In order for VBA code to run automatically, there has to be some "event" that triggers it to run, like the opening of a file, the selection of a particular cell, or the update of a particular cell.
What would be the "event" to automatically trigger the VBA to run in this case?

The other option is to run the VBA code on command.

I guess the event could be whatever you recommend is easiest. pressing a button or clicking on a cell will do. I’m open to whatever is simplest to code?
 
Upvote 0
Try this code.
You can assign it to a command button, or keyboard shortcut, if you like:
VBA Code:
Sub MyInsertRows()

    Dim lr As Long
    Dim r As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Loop through rows backwards
    For r = lr To 3 Step -1
'       Compare value in column B with value in row above it
        If Cells(r, "B") <> Cells(r - 1, "B") Then
            Rows(r & ":" & r + 1).EntireRow.Insert
        End If
    Next r
        
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
Try this code.
You can assign it to a command button, or keyboard shortcut, if you like:
VBA Code:
Sub MyInsertRows()

    Dim lr As Long
    Dim r As Long
   
    Application.ScreenUpdating = False
   
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
   
'   Loop through rows backwards
    For r = lr To 3 Step -1
'       Compare value in column B with value in row above it
        If Cells(r, "B") <> Cells(r - 1, "B") Then
            Rows(r & ":" & r + 1).EntireRow.Insert
        End If
    Next r
       
    Application.ScreenUpdating = True
   
End Sub

This is my first time working with VBA. I had to research how to add a button and associate code to it, but I didn't screw that part up because your code worked flawlessly! Thank you so much.

Now to understand what the code is doing... where and/or what would I change it if I needed to amend the number of blank rows to add. Let's say, if I'd rather have 1 row inserted instead of two?
 
Upvote 0
where and/or what would I change it if I needed to amend the number of blank rows to add. Let's say, if I'd rather have 1 row inserted instead of two?

For more than two rows, just change the "1" (i.e. 2 would make 3 rows inserted, 3 would make 4, etc).
VBA Code:
Rows(r & ":" & r + 1).EntireRow.Insert

For one row, just change that line to this:
VBA Code:
Rows(r).EntireRow.Insert
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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