Book1 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | 1 | cat | ||||||||
2 | 2 | dog | ||||||||
3 | 3 | mouse | ||||||||
4 | 1 | |||||||||
5 | 2 | |||||||||
6 | 3 | |||||||||
7 | ||||||||||
8 | 1 | cat | ||||||||
9 | 1 | |||||||||
10 | 2 | dog | ||||||||
11 | 2 | |||||||||
12 | 3 | mouse | ||||||||
13 | 3 | |||||||||
14 | ||||||||||
Sheet1 |
I needed similar code some time ago and used this. Make sure you save your work before you test this, but if you place your cursor at the start of the column you wish to check for data, this code will insert one row each time a non-blank cell is encountered. The code will only run for the first 20 rows of data.
Give it a try and hopefully it will be what you are looking for.
Sub insertrow()
' insertrow Macro
Application.ScreenUpdating = True
Dim count As Integer
Dim X As Integer
For count = 1 To 20
If activecell.Value <> "" Then
activecell.Offset(1, 0).Select
Range(activecell, activecell.Offset(0, 0)).EntireRow.Insert
activecell.Offset(1, 0).Select
For X = 1 To 1
Next X
Else
activecell.Offset(1, 0).Range("a1").Select
End If
Next count
End Sub
Neville.
Sub InsertRowsBefore()
Dim MyCell As Range
Dim SelectionHeight As Long
Dim MyCount As Long
Range("A2:A11").Select 'I used this for testing purposes
SelectionHeight = Selection.Rows.Count
'Allow for selection starting in Row 1
If Selection.Cells(1).Row = 1 Then
Selection.Cells(1).EntireRow.Insert
Selection.Offset(1, 0).Resize(SelectionHeight, 1).Select
Else
Selection.Cells(1).Offset(-1, 0).EntireRow.Insert
End If
'Row 2 and subsequent
SelectionHeight = Selection.Rows.Count
Selection.Cells(1).Select
For MyCount = 1 To SelectionHeight
'MsgBox ("Current Row: " & Selection.Row)
If Selection.Row = 3 Then
Selection.Offset(1, 0).Resize(1, 1).Select
Selection.Insert
Else
Selection.Offset(2, 0).Resize(1, 1).Select
Selection.Offset(-1, 0).EntireRow.Insert
End If
Next
End Sub
Sub InsertRowsAfter()
Dim MyCell As Range
For Each MyCell In Selection
If MyCell.Value <> "" Then
MyCell.Offset(1, 0).EntireRow.Insert
End If
Next MyCell
End Sub