VBA - Insert A Row before every month

drcoz

New Member
Joined
Mar 7, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet for when employee's birthdays are. I've gotten everything but now I need to insert the name of the Month on a line before the month and looking for some VBA code to do this. I'm using 365 Excel and can not use a macro due to computer security but I can use VBA.

My raw data looks like this:
Capture1.JPG


The End state I want is this:

Capture2.JPG


The empty columns I need, I just removed the data for privacy concerns.

As you can see I'm able to sort the list just stuck on how to insert the Month lines in my final example. I've done several searches on line but the codes I find don't do what I want or don't work at all.

Thanks for your assistance and time.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
try something like this:
VBA Code:
Private Sub insertline()
Dim rng As Range
Dim cell As Range

Set rng = Sheet12.Range("D2:D50")

For Each cell In rng
 
 If Left(cell.Value, 3) <> Left(cell.Offset(-1, 0).Value, 3) And cell.Offset(-1, 0).Value <> "" Then
   cell.EntireRow.Insert
 End If
Next cell
End Sub
 
Upvote 0
It sort of worked but not the way I need it to work.

I need to keep all of Jan together, then a line break, then all of Feb. Currently it's breaking the month of Jan more, see below:

Capture3.JPG


I also need when the line is added to automatically add the month name, like JAN, FEB, MAR, etc...
 
Upvote 0
Here's updated code to add the month in column A. As for why it's splitting January...I wonder if there are spaces before the text. To rule that out, I have added some trims. Try this:
VBA Code:
Private Sub insertline()
Dim rng As Range
Dim cell As Range

Set rng = Sheet12.Range("D2:D30")

For Each cell In rng
 
 If Left(Trim(cell.Value), 3) <> Left(Trim(cell.Offset(-1, 0).Value), 3) And cell.Offset(-1, 0).Value <> "" Then
   cell.EntireRow.Insert
   cell.Offset(-1, -3).Value = Left(Trim(cell.Value), 3)
 End If
Next cell
End Sub
 
Upvote 0
No, still putting in too many spaces and putting month and day on the blank line as well.

Capture4.JPG
 
Upvote 0
Okay, the problem is that it was reading the dates in numerical format. Give this a go:
VBA Code:
Private Sub insertline()
Dim rng As Range
Dim cell As Range

Set rng = Sheet12.Range("D2:D30")

For Each cell In rng
 If cell.Offset(-1, 0).Value <> "" Then
   If Left(Format(cell.Value, "MMM"), 3) <> Left(Format(cell.Offset(-1, 0).Value, "MMM"), 3) Then
     cell.EntireRow.Insert
     cell.Offset(-1, -3).Value = Left(Format(cell.Value, "MMM"), 3)
   End If
 End If
Next cell
End Sub
 
Upvote 0
Solution
BINGO! Thanks worked! Thanks for the help on this one!!!
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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