Inserting Rows Base on Data Above or Below Row

Big_Chew

New Member
Joined
Oct 15, 2020
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hey guys,

I am looking for help with some VBA code to insert a row based on the data either above or below the row. In the data set I have, column A contains the year and column B contains the month. I can have more than two rows of the same month and year, so I may have more than 12 rows containing the same year. What I am wanting to achieve is insert one row (two would be best) between years 2020 and 2021 and 2022 and so on. Can anyone help?
 

Attachments

  • 1602786390811.png
    1602786390811.png
    12.7 KB · Views: 16

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub BigChew()
   Dim i As Long
   
   For i = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
      If Cells(i, 1) <> Cells(i - 1, 1) Then Rows(i).Resize(2).Insert
   Next i
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
After further testing I have a slight issue.

How do I get the code to only run a certain sheet?
 
Upvote 0
What is the name of the sheet?
 
Upvote 0
Ok, how about
VBA Code:
Sub BigChew()
   Dim i As Long
   
   With Sheets("Adjusted")
      For i = .Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
         If .Cells(i, 1) <> .Cells(i - 1, 1) Then .Rows(i).Resize(2).Insert
      Next i
   End With
End Sub
 
Upvote 0
Thank you so much. I tried other code I had to only focus on the Adjusted which worked with other equations but I could not get it to work with the insert cells.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,695
Members
449,117
Latest member
Aaagu

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