Try this (it assumes that your data is in column A).
Sub Insert_row()
Dim Number_of_rows
Number_of_rows = ActiveSheet.UsedRange.Rows.Count
Range("A2").Select
Do Until Selection.Row = Number_of_rows + 1
If Selection.Value <> Selection.Offset(-1, 0).Value Then
Selection.EntireRow.Insert
Number_of_rows = Number_of_rows + 1
Selection.Offset(2, 0).Select
Else
Selection.Offset(1, 0).Select
End If
Loop
End Sub
Hi Barry
As I don't generally use Loops due their slow running, you can do this (Insert rows) with in one step:
Sub Placeinrows()
Dim ListRange As Range
Set ListRange = Range("A2:A1000")
ListRange.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set ListRange = _
ListRange.SpecialCells(xlCellTypeVisible)
ActiveSheet.ShowAllData
ListRange.EntireRow.Insert
Set ListRange = Nothing
End Sub
OzGrid Business Applications
Dave
Avoiding loops is a good practice, but does your macro work if an animal name appears only once?
For example : Dog,Dog,Dog,Cat,Mouse,Cow,Horse,Horse.
Sylvester
Dave Avoiding loops is a good practice, but does your macro work if an animal name appears only once? For example : Dog,Dog,Dog,Cat,Mouse,Cow,Horse,Horse. Sylvester
Now Sylvester you know the answer don't you :o) ?
You are dead right though ! I should not have made the assumption there were no singles.
If this is the case I would use the dreaded loop BUT! Not on all entries, I would first narrow down the range to loop through to only the cells where the animal changes. Like below:
Sub Placeinrows()
Dim ListRange As Range
Dim i As Integer, Rw As Integer
Application.ScreenUpdating = False
Set ListRange = Range("A2:A10000")
ListRange.Offset(0, 1) = _
"=IF(RC[-1]<>R[-1]C[-1],NA(),"""")"
Set ListRange = _
ListRange.Offset(0, 1).SpecialCells(xlCellTypeFormulas, xlErrors)
For i = ListRange.Areas.Count To 1 Step -1
For Rw = ListRange.Areas(i).Rows.Count To 1 Step -1
ListRange.Areas(i).Rows(Rw).EntireRow.Insert (xlShiftDown)
Next Rw
Next i
Columns(2).Clear
Application.ScreenUpdating = True
Set ListRange = Nothing
End Sub
What do you think ?
Dave
OzGrid Business Applications