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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,436
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
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

chris186h

Board Regular
Joined
Mar 18, 2015
Messages
111
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,196,017
Messages
6,012,872
Members
441,737
Latest member
bijayche

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
Top