ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Insert lines when x< y (https://www.excelbanter.com/excel-programming/318365-insert-lines-when-x-y.html)

Teresa

Insert lines when x< y
 
I have a column of numbers as below:

1
1
1
2
2
2
3
3
........

I am writing a macro to insert lines between each didtinct number,
the code below isnt quite working - I am missing 1/2 lines somewhere,
thks in advance

Sub li()

Dim i
For i = 1 To 10
If Cells(i + 1, 5).Value < Cells(i, 5).Value Then
Cells(i + 1, 5).EntireRow.Insert
End If
Next


End Sub


Tom Ogilvy

Insert lines when x< y
 
If your numbers are in Column E then:

Sub li()
Dim LastRow As Long
Dim i As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = LastRow - 1 To 1 Step -1
If Cells(i + 1, 5).Value < Cells(i, 5).Value Then
Cells(i + 1, 5).EntireRow.Insert
End If
Next
End Sub



--
Regards,
Tom Ogilvy




"teresa" wrote in message
...
I have a column of numbers as below:

1
1
1
2
2
2
3
3
.......

I am writing a macro to insert lines between each didtinct number,
the code below isnt quite working - I am missing 1/2 lines somewhere,
thks in advance

Sub li()

Dim i
For i = 1 To 10
If Cells(i + 1, 5).Value < Cells(i, 5).Value Then
Cells(i + 1, 5).EntireRow.Insert
End If
Next


End Sub




Tom Ogilvy

Insert lines when x< y
 
correction:

Sub li()
Dim LastRow As Long
Dim i As Long
LastRow = Cells(Rows.Count, 5).End(xlUp).Row
For i = LastRow - 1 To 1 Step -1
If Cells(i + 1, 5).Value < Cells(i, 5).Value Then
Cells(i + 1, 5).EntireRow.Insert
End If
Next
End Sub

Changed the LastRow line to look in column 5 as well.

--
Regards,
Tom Ogilvy


"teresa" wrote in message
...
I have a column of numbers as below:

1
1
1
2
2
2
3
3
.......

I am writing a macro to insert lines between each didtinct number,
the code below isnt quite working - I am missing 1/2 lines somewhere,
thks in advance

Sub li()

Dim i
For i = 1 To 10
If Cells(i + 1, 5).Value < Cells(i, 5).Value Then
Cells(i + 1, 5).EntireRow.Insert
End If
Next


End Sub





All times are GMT +1. The time now is 09:27 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com