Insert new row based on a cells date

skpma

New Member
Joined
Mar 3, 2020
Messages
31
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hello everyone

I was searching around, but wasn't able to find a solution for my problem.

I have the following list:
Datalist.xlsx
ABC
1Title ATitle BDate
2123456A2020/12/7
31234567B2020/12/15
4123456778C2021/1/1
587654321M2021/1/1
6987654321G2021/12/14
Sheet1


Now what I need is to check the cell value in column C if the entries are still in the same month, if not add a new row above that cell and write the corresponding month into column A.
It should look like this:
Book1
ABC
1Title ATitle BDate
2December
3123456A2020/12/7
41234567B2020/12/15
5January
6123456778C2021/1/1
787654321M2021/1/1
8April
9987654321G2021/4/14
Sheet1



I found the following code, which adds new rows if the date changes, but I can't make it work to check only the month.


VBA Code:
Sub InsertAtDateChange()
Dim lastRow, chkRw As Integer
 lastRow = Range("C" & Rows.Count).End(xlUp).Row
   For chkRw = lastRow To 1 Step -1
     If Range("C" & chkRw) <> Range("C" & chkRw + 1) Then
        Range("C" & chkRw + 1).EntireRow.Insert shift:=xlDown
     End If
   Next
End Sub

Any help is appreciated. Thanks in advance
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
See if this does what you want:

VBA Code:
Sub ta()
Dim i As Long
With ActiveSheet
    For i = .Cells(Rows.Count, 3).End(xlUp).Row To 2 Step -1
        If Month(.Cells(i, 3).Value) <> Month(.Cells(i - 1, 3).Value) And Not Application.IsText(.Cells(i - 1, 1)) Then
            Rows(i).Insert
            .Cells(i, 1) = Format(.Cells(i + 1, 3).Value, "mmmm")
        End If
    Next
End With
End Sub
 
Upvote 0
See if this does what you want:
Thank you for your reply.
Unfortunately I get an VB error stating that the format is not correct. If I delete the title header it does nothing. Do I have to change some formats?
 
Upvote 0
Sorry for doubleposting...

If I remove the line below and also delete the header row, than it does almost what I want except it does not do it for the first month.
VBA Code:
And Not Application.IsText(.Cells(i - 1, 1))

Anything I can do about this? (removing the header line is not an option unfortunately)
 
Upvote 0
How about inserting a blank row under the header row as a quick fix. I thought that might be an issue but couldn't think of a way around it off hand. Maybe do the row insert in the code before it starts the For i loop? Then column A would fill on that row when the loop runs.
Rich (BB code):
Sub ta()
Dim i As Long
With ActiveSheet
    Rows(2).Insert
    For i = .Cells(Rows.Count, 3).End(xlUp).Row To 2 Step -1
        If Month(.Cells(i, 3).Value) <> Month(.Cells(i - 1, 3).Value)  Then
            Rows(i).Insert
            .Cells(i, 1) = Format(.Cells(i + 1, 3).Value, "mmmm")
        End If
    Next
End With
End Sub
 
Upvote 0
Solution
How about inserting a blank row under the header row as a quick fix. I thought that might be an issue but couldn't think of a way around it off hand. Maybe do the row insert in the code before it starts the For i loop? Then column A would fill on that row when the loop runs.
Thanks for your reply, but it stills throws an error.
It's probably not good coding but I got it working with the following:

VBA Code:
Sub ta()
Dim i As Long
With ActiveSheet
    'Rows(2).Insert
    For i = .Cells(Rows.Count, 3).End(xlUp).Row To 2 Step -1
        On Error Resume Next
        If Month(.Cells(i, 3).Value) <> Month(.Cells(i - 1, 3).Value) Then
            Rows(i).Insert
            .Cells(i, 1) = Format(.Cells(i + 1, 3).Value, "mmmm")
        End If
    Next
End With
End Sub

I will go with this for now. Thanks for your help!
 
Upvote 0
If It is producing what you want then the coding is OK.
Thanks for the feedbacfk,
regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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