Inserting a row based on a condition

Andries

Board Regular
Joined
Feb 3, 2011
Messages
127
Hi

I want to insert a row if a certain condition is met...for e.g. I want to insert a row between "CLOSED" and ""O/B"

Thank you




Excel Workbook
GHI
16CodeOpen/Closeddata
17206CLOSED32
18206CLOSED39
19*CLOSED3
20*CLOSED8
21*O/B48
22252O/B28
23252O/B124
REQUEST FORM
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi....
Check this out...!!!!


Private Sub CommandButton1_Click()
R1 = 2
Do While ActiveSheet.Cells(R1, 2).Value <> ""
Closed_O_B = ActiveSheet.Cells(R1, 2).Value
If Closed_O_B = "CLOSED" Or Closed_O_B = "O/B" Then
ActiveSheet.Rows(R1 + 1).Select
Selection.Insert Shift:=x1Down, CopyOrigin:=xlFormatFromLeftOrAbove
R1 = R1 + 1
End If
R1 = R1 + 1
Loop
End Sub
 
Upvote 0
I want to insert a row if a certain condition is met...for e.g. I want to insert a row between "CLOSED" and ""O/B"
I suspect that we shouldn't need to loop through every row. A bit more detail about what the data is like, or could be like, would help get an efficient answer.

If we find the first occurrance of "O/B", can we just add a row above that?

Or do we need to check that the cell above contains "CLOSED" as well?

Could your columns look like this?
CLOSED
OPEN
O/B
CLOSED
O/B

or this?
OPEN
OPEN
O/B
CLOSED
O/B

etc
 
Upvote 0
Dries

Perhaps you missed my questions but for your sample data, it could be a one-pass code like this.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> InsertRow()<br>    <SPAN style="color:#00007F">Dim</SPAN> OB <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">With</SPAN> Range("H16", Range("H" & Rows.Count).End(xlUp))<br>        <SPAN style="color:#00007F">Set</SPAN> OB = .Find(What:="O/B", After:=.Cells(1, 1), LookIn:= _<br>            xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, _<br>            MatchCase:=False, SearchFormat:=False)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> OB <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        OB.EntireRow.Insert<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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