easy macro to insert rows?

neov

Board Regular
Joined
Oct 3, 2003
Messages
67
My rust is showing....

I have a spreadsheet that has multiple columns...column B has either a 1, a 2, or an "#N/A".

I need to insert a row under any row that has a "2" in Column B.

I need to do this on every worksheet in the spreadsheet.

Any way to do this? Thanks in advance.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
try
Code:
Sub addrow()
Dim lr As Long, r As Long, ws As Worksheet
    For Each ws In Worksheets
        ws.Activate
        lr = Cells(Rows.Count, "B").End(xlUp).Row
            For r = lr To 1 Step -1
                If Range("B" & r).Value = 2 Then
                    Rows(r + 1).EntireRow.Insert
                End If
            Next r
     Next ws
End Sub
 
Upvote 0
getting a 'type mismatch' error message on this line:

If Range("I" & r).Value = 2 Then


I was wrong about the column - Column I is the column in question, not column B, so I changed the B to an I on both lines
 
Upvote 0
Give this macro a try (it should execute quite quickly)...
Code:
Sub InsertRowUnder2s()
  Application.ScreenUpdating = False
  With Columns("I")
    .Replace 2, "TRUE", xlWhole
    With .SpecialCells(xlCellTypeConstants, xlLogical)
      .Offset(1).EntireRow.Insert
      .Value = 2
    End With
  End With
  Application.ScreenUpdating = True
End Sub
EDIT NOTE: I updated the above code to reflect the column change you mentioned in one of your responses.
 
Upvote 0
Changing the "B" to an "I" should make no difference, as long as you did use an "I" and not a 1....and make sure that the 2 is a number and not a text "2"
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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