Insert rows

[Closed]
Report
-
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
-
Hello,

I have a question about "if End If" nested in a For Next structure in excel vba.

I have been trying to write a code but I cannot get the correct syntax.

I have a column labeled "names" a I want to insert a blank row when the names in consecutive rows (downward) differ. For example, you start the name list with:
"Mary" (cell A1)
"Mary"(cell A2)
"Patricia" (cell A3)

After using the codeI would like that the name "Patricia" will be located in cell A4, and a blank row is inserted on row 3.

Up to this point I have written the following code:

Sub InsertColumn()

Dim Row As Integer

For Row = 1 To 10 Step 1

If Cells(Row, 1).Text <> Cells(Row + 1, 1).Text Then
Cells(Row, 1).EntireRow.Select

End If
Next Row

End Sub


Hopefully I have explained myself. Can anyone help me?

Thanks,

Arié.

1 reply

Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
Sub InsertColumn()

Dim Row As Integer

    For Row = 10 To 1 Step -1
    
        If (Row = 1) Then
            
            Cells(Row, 1).EntireRow.Insert
            
        ElseIf (Cells(Row, 1).Text <> Cells(Row - 1, 1).Text) Then
        
            Cells(Row, 1).EntireRow.Insert
                
        End If
    Next Row

End Sub