Help inserting row when condition is met

Closed
Report
-
Posts
2829
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
November 22, 2021
-
Hello,

I am trying to write a macro that would insert a row when the cell contains either, "Total", "Male" or "White". There are 3 sets of "Total", "Male" or "White" in each section of data.

All of the data is in Column "C".

This code works, EXCEPT for some reason it is not working correctly in the last section of data (last 39 rows of my data), and is not consistent with the way it is not working. In the first set, it is inserting a row above "Total" and "Male".
In the second set, it is inserting a row above "Total" and "Male".
In the third set, it is inserting a row above "Total" only.

This is the code:

Sub InsKeyWord()
Dim i As Long
Dim LastRow As Long
Dim Arr As Variant
LastRow = Cells(Rows.Count, "C").End(xlUp).Row
Arr = Array("Total", "Male", "White")
For x = 0 To UBound(Arr)
For i = LastRow To 2 Step -1
With Cells(i, "C")
If .Value Like Arr(x) Then
.Resize(1, 1).EntireRow.Insert
End If
End With
Next i
Next x

End Sub


Please help! Thanks!

1 reply

Posts
2829
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
November 22, 2021
490
Hi Stephanie,

Have you checked your capitols; "Total" is not the same as "total".

Best regards,
Trowa