Find a Partial text and Insert A Column Above The Text

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

Can you help me tweak the code below for below table?

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]No[/TD]
[TD]x[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Sean[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Output Submitted To Department[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Output Submitted To Deparmnt[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Output Sbmitted To Dpartment[/TD]
[/TR]
</tbody>[/TABLE]


-------------------------------------------------------------------
Sub AddRow()


Dim SrchRng As Range, cel As Range


Set SrchRng = Range("B:B")


For Each cel In SrchRng
If InStr(1, cel.Value, "OUTPUT") > 0 Then
cel.Offset(1, 0).EntireRow.Insert
End If
Next cel


End Sub

---------------------------------------------------------------------

The code is working but I wanted to insert a column on the FIRST found "OUTPUT" word in B column and not repetitively add in every word found.


Any help will be much appreciated.
 

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.
Try

Code:
Sub AddRow()
Dim SrchRng As Range, cel As Range, s as Integer
Set SrchRng = Range("B:B")
s = 0
For Each cel In SrchRng
    If s = 1 Then Exit Sub
        If InStr(1, cel.Value, "Output") > 0 Then
            cel.Offset(1, 0).EntireRow.Insert
            s = s + 1
    End If
Next cel
End Sub
 
Upvote 0
The code is working but need to insert the row above the word "Output". Thanks for the help. :)
 
Upvote 0
Try

Code:
Sub AddRow()
Dim SrchRng As Range, cel As Range, s as Integer
Set SrchRng = Range("B:B")
s = 0
For Each cel In SrchRng
    If s = 1 Then Exit Sub
        If InStr(1, cel.Value, "Output") > 0 Then
            cel.Offset(-1, 0).EntireRow.Insert
            s = s + 1
    End If
Next cel
End Sub
 
Upvote 0
Thanks Michael but additional row is inserted above the data next to "Output".

It looks like this:

Cell B12: Inserted Blank
Cell B13: Sean
Cell B14: Output

Should be:

Cell B12: Sean
Cell B13: Inserted Blank
Cell B14: Output

Thanks again!
 
Upvote 0
Try then

Code:
Sub AddRow()
Dim SrchRng As Range, cel As Range, s As Integer
Dim lr As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
Set SrchRng = Range("B1:B" & lr)
s = 0
For Each cel In SrchRng
    If s = 1 Then Exit Sub
        If InStr(1, cel.Value, "Output") > 0 Then
            cel.EntireRow.Insert
            s = s + 1
    End If
Next cel
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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