Insert new row if Matching word is found

chris186h

Board Regular
Joined
Mar 18, 2015
Messages
111
Hello All.
The code below is sort of what im after, although instead of inserting a row when it finds a match i want it to insert a row if it contains the word "Total".

Code:
Dim cell As RangeFor Each cell In Range("B2", Cells(Rows.Count, "B").End(xlUp).Address)
'loop from B1 to the last used cell in column B
    If cell.Offset(-1, 0).Value2 <> cell.Value2 And cell.Offset(-1, 0) <> vbNullString Then cell.EntireRow.Insert
    'if the cell above is different and not null then it adds a row above
Next cell

Thankyou for all your help in advance
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
try

Code:
Dim rng as range
For each rng in Range("B2", Cells(Rows.Count, "B").End(xlUp).Address)
If Instr(Ucase(rng.Value),"TOTAL")>0 Then rng.EntireRow.Insert Shift:=xlDown
next rng
 
Upvote 0
Thankyou for your response. However this seams to keep inserting multiple rows above the cell containing total and does not move onto the next match.

try

Code:
Dim rng as range
For each rng in Range("B2", Cells(Rows.Count, "B").End(xlUp).Address)
If Instr(Ucase(rng.Value),"TOTAL")>0 Then rng.EntireRow.Insert Shift:=xlDown
next rng
 
Upvote 0

Forum statistics

Threads
1,213,486
Messages
6,113,932
Members
448,533
Latest member
thietbibeboiwasaco

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