Give it a try.
Explanation:
- You should avoid using
.Select/ActiveCell
, etc. You might want to see this link - When working with the last line, it is better not to hard code the values, but to dynamically find the last line. You might want to see this link
- Working with objects if the current sheet is not a sheet that you want to work with?
- The next FOR loop will cross the line from the bottom and move up.
The code:
Sub Sample() Dim ws As Worksheet Dim LRow As Long, i As Long Dim insertRange As Range '~~> Chnage this to the relevant sheet Set ws = ThisWorkbook.Sheets("Sheet1") '~~> Work with the relevant sheet With ws '~~> Get the last row of the desired column LRow = .Range("E" & .Rows.Count).End(xlUp).Row '~~> Loop from last row up For i = LRow To 1 Step -1 '~~> Check for the condition '~~> UCASE changes to Upper case '~~> TRIM removes unwanted space from before and after If UCase(Trim(.Range("E" & i).Value)) = "SAME DATE" Then '~~> Insert the rows .Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove End If Next i End With End Sub
Screenshot:
Subsequent observation of comments
It really worked! BUT, one final modification. in your code: Set ws = ThisWorkbook.Sheets ("Sheet1") Is it possible that you can set WS as the active sheet. The reason for this is because the worksheet name is unique and incompatible as well.
As I mentioned, in the first link above, as well as in the comment, do not use Activesheet
. Use CodeNames
sheet that does not change. See screenshot below.
Blah Blah
is the name of the sheet that you see on the worksheet tab, but Sheet1
is the CodeName
that will not change. those. you can change the sheet name from Blah Blah
to say Kareen
, but in the VBA editor you will notice that CodeName
does not change :)
Change code
Set ws = ThisWorkbook.Sheets("Sheet1")
to
'~~> Replace Sheet1 with the relevant Code Name Set ws = [Sheet1]
source share