Inserting Rows with IF/THEN in a macro

ncheatherp

New Member
Joined
Dec 30, 2010
Messages
7
I'm trying to figure out how to write the following logic inside a macro:

IF W19=Y, THEN INSERT 2 ROWS BELOW R19
IF W19=Y AND X19=Y, THEN INSERT 5 ROWS BELOW R19
Then I would like it to loop to the next row of data and do the same thing (until it gets thru each row of data). For example, if 5 Rows were added after R19, it would then look at W26 & X26 next.

I know that anything is possible....this just has me stumped!! :)
 
Welcome to the board.

Does this loop start on w19, or does it start at w1 or something? if w19=y and x19=y, then you want to add 7 rows?
 
Upvote 0
Welcome to MrExcel Board...

are you truly inserting rows or just cells? The reason I ask is because you say to insert rows at a cell location?
 
Upvote 0
If you're adding rows, try this one out.

Code:
Sub insert()
'
' insert Macro
'

'
LastRow = Cells(Rows.Count, 23).End(xlUp).Row
For i = LastRow To 1 Step -1
   If Range("w" & i).Value = "Y" Then
      Rows(i + 1).insert shift:=xlDown
      Rows(i + 1).insert shift:=xlDown
         If Range("x" & i).Value = "Y" Then
            Rows(i + 1).insert shift:=xlDown
            Rows(i + 1).insert shift:=xlDown
            Rows(i + 1).insert shift:=xlDown
            Rows(i + 1).insert shift:=xlDown
            Rows(i + 1).insert shift:=xlDown
        End If
    End If
Next i
End Sub
 
Last edited:
Upvote 0
here is my version

Code:
Sub InsertRows()
Dim LR As Long
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 19 Step -1
If Cells(i, "X") = "Y" And Cells(i, "W") = "Y" Then
    Rows(i & ":" & i + 5).EntireRow.Insert
    ElseIf Cells(i, "W") = "Y" Then
    Rows(i & ":" & i + 2).EntireRow.Insert
End If
Next i
 
End Sub
 
Last edited:
Upvote 0
Thanks for the welcomes! :)

I truly want it to insert rows.

W (starting with W19) contains Ys and Ns (for Yes or No)
X (starting wtih X19) also contains Ys and Ns (for Yes or No)

If the value in W is equal to "Y" and the value in X is also equal to "Y", I want to insert 5 rows.

If the value in W is equal to "Y" and the value in X is "N", I only want to insert 2 rows.

I apologize if I seem to be a novice... I am! :) I volunteered to take on writing a macro to automatically create a spreadsheet that was built manually in the past.... a spreadsheet that took 20 hours per month to build manually. I've spent 2 days on this so far and am almost there. This issue has me stumped from going any further tho.

I tried the code you provided and it didn't work.
 
Upvote 0
What code did you try? And what does the code do?

Are you sure the data in the cell is only "Y" or are there perhaps spaces?
 
Upvote 0
Code:
Sub InsertRows()
Dim LR As Long
LR = ActiveSheet.Range("W" & Rows.Count).End(xlUp).Row
For i = LR To 19 Step -1
If Cells(i, "W") = "Y" or "y" Then
    Rows(i + 1 & ":" & i + 2).EntireRow.Insert
        If Cells(i, "X") = "Y" or "y" Then
             Rows(i + 1 & ":" & i + 3).EntireRow.Insert
        end if
End If
Next i
 
End Sub
there is his code, but with the appropriate tweak. NOW you can use upper or lower case Ys
 
Last edited:
Upvote 0

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