VBA to insert row based on criteria

Spartan300

Board Regular
Joined
Jul 1, 2008
Messages
71
Hi,

I have a sheet of around 5000 rows, I would like a macro to do some manual work for me.

Is it possible for a macro to insert a row if a "1" is present in column I. If possible I would like the row to be inserted above the 1.

Thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
In column I, the numbers are relevant to the number of lines an order has, for example if an order has four lines and the next order two lines the numbers will appear as follows...

<TABLE style="WIDTH: 17pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=23 border=0><COLGROUP><COL style="WIDTH: 17pt; mso-width-source: userset; mso-width-alt: 736" width=23><TBODY><TR style="HEIGHT: 21pt; mso-height-source: userset" height=28><TD class=xl63 style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; WIDTH: 17pt; BORDER-BOTTOM: #e0dfe3; HEIGHT: 21pt; BACKGROUND-COLOR: transparent" align=right width=23 height=28>1</TD></TR><TR style="HEIGHT: 21pt; mso-height-source: userset" height=28><TD class=xl63 style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 21pt; BACKGROUND-COLOR: transparent" align=right height=28>2</TD></TR><TR style="HEIGHT: 21pt; mso-height-source: userset" height=28><TD class=xl63 style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 21pt; BACKGROUND-COLOR: transparent" align=right height=28>3</TD></TR><TR style="HEIGHT: 21pt; mso-height-source: userset" height=28><TD class=xl63 style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 21pt; BACKGROUND-COLOR: transparent" align=right height=28>4</TD></TR><TR style="HEIGHT: 21pt; mso-height-source: userset" height=28><TD class=xl63 style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 21pt; BACKGROUND-COLOR: transparent" align=right height=28>1</TD></TR><TR style="HEIGHT: 21pt; mso-height-source: userset" height=28><TD class=xl63 style="BORDER-RIGHT: #e0dfe3; BORDER-TOP: #e0dfe3; BORDER-LEFT: #e0dfe3; BORDER-BOTTOM: #e0dfe3; HEIGHT: 21pt; BACKGROUND-COLOR: transparent" align=right height=28>2</TD></TR></TBODY></TABLE>

These are all in separate rows and are located in column I, so what I would like the macro to do is insert a row above the 1 each time a 1 appears.

Thanks
 
Upvote 0
Dim c As Range, rng

Set rng = Range("i2:i" & Range("i65536").End(xlUp).Row)

For Each c In rng

If InStr(c, "1") Then

c.Offset(1, 0).EntireRow.Insert shift:=xlDown

End If
Next c

For Each c In rng
If c.Value = "" Then
c = c.Offset(-1, 0)
c.Offset(-1, 0) = ""
End If
Next c
 
Upvote 0
Hi,

Thanks very much for the code, I have a problem though.

I have data before the column I, a row is being inserted from column I to colun IV after this the loop is then inserting another row up to column I.
Because of this my data is slightly out of sync.

I have tried to amend the code but failed horribly.

Thanks
 
Upvote 0
Dim c As Range, rng

Set rng = Range("i2:i" & Range("i65536").End(xlUp).Row)

For Each c In rng

If InStr(c, "1") Then

c.Offset(1, 0).EntireRow.Insert shift:=xlDown

End If
Next c

For Each c In rng
If c.Value = "" Then
c.Offset(-1, 0).EntireRow.Copy
c.EntireRow.Select
ActiveSheet.Paste
c.Offset(-1, 0).EntireRow.ClearContents

End If
Next c
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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