Insert rows

Closed
Arié - May 30, 2010 at 12:29 AM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - May 30, 2010 at 03:18 AM
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 response

rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
May 30, 2010 at 03:18 AM
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
0